xref: /openbsd/sys/dev/ic/aic79xx_openbsd.c (revision 404b540a)
1 /*	$OpenBSD: aic79xx_openbsd.c,v 1.32 2009/02/16 21:19:06 miod 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 <sys/cdefs.h>
64 /*
65 __FBSDID("$FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.16 2003/12/17 00:02:09 gibbs Exp $");
66 */
67 
68 #include <dev/ic/aic79xx_openbsd.h>
69 #include <dev/ic/aic79xx_inline.h>
70 #include <dev/ic/aic79xx.h>
71 
72 #ifndef AHD_TMODE_ENABLE
73 #define AHD_TMODE_ENABLE 0
74 #endif
75 
76 /* XXX milos add ahd_ioctl */
77 int	ahd_action(struct scsi_xfer *);
78 int	ahd_execute_scb(void *, bus_dma_segment_t *, int);
79 int	ahd_poll(struct ahd_softc *, int);
80 int	ahd_setup_data(struct ahd_softc *, struct scsi_xfer *,
81 		    struct scb *);
82 
83 void	ahd_adapter_req_set_xfer_mode(struct ahd_softc *, struct scb *);
84 void    ahd_minphys(struct buf *, struct scsi_link *);
85 
86 struct cfdriver ahd_cd = {
87 	NULL, "ahd", DV_DULL
88 };
89 
90 static struct scsi_adapter ahd_switch =
91 {
92 	ahd_action,
93 	ahd_minphys,
94 	0,
95 	0,
96 };
97 
98 /* the below structure is so we have a default dev struct for our link struct */
99 static struct scsi_device ahd_dev =
100 {
101 	NULL, /* Use default error handler */
102 	NULL, /* have a queue, served by this */
103 	NULL, /* have no async handler */
104 	NULL, /* Use default 'done' routine */
105 };
106 
107 /*
108  * Attach all the sub-devices we can find
109  */
110 int
111 ahd_attach(struct ahd_softc *ahd)
112 {
113 	struct scsibus_attach_args saa;
114 	char   ahd_info[256];
115 	int	s;
116 
117 	ahd_controller_info(ahd, ahd_info, sizeof ahd_info);
118 	printf("%s\n", ahd_info);
119 	ahd_lock(ahd, &s);
120 
121 	/*
122 	 * fill in the prototype scsi_links.
123 	 */
124 	ahd->sc_channel.adapter_target = ahd->our_id;
125 	if (ahd->features & AHD_WIDE)
126 		ahd->sc_channel.adapter_buswidth = 16;
127 	ahd->sc_channel.adapter_softc = ahd;
128 	ahd->sc_channel.adapter = &ahd_switch;
129 	ahd->sc_channel.openings = 16;
130 	ahd->sc_channel.device = &ahd_dev;
131 
132 	if (bootverbose) {
133 		ahd_controller_info(ahd, ahd_info, sizeof ahd_info);
134 		printf("%s: %s\n", ahd->sc_dev.dv_xname, ahd_info);
135 	}
136 
137 	ahd_intr_enable(ahd, TRUE);
138 
139 	if (ahd->flags & AHD_RESET_BUS_A)
140 		ahd_reset_channel(ahd, 'A', TRUE);
141 
142 	bzero(&saa, sizeof(saa));
143 	saa.saa_sc_link = &ahd->sc_channel;
144 
145 	ahd->sc_child = config_found((void *)&ahd->sc_dev, &saa, scsiprint);
146 
147 	ahd_unlock(ahd, &s);
148 
149 	return (1);
150 
151 }
152 
153 /*
154  * Catch an interrupt from the adapter
155  */
156 int
157 ahd_platform_intr(void *arg)
158 {
159 	struct	ahd_softc *ahd;
160 
161 	/* XXX in ahc there is some bus_dmamap_sync(PREREAD|PREWRITE); */
162 
163 	ahd = (struct ahd_softc *)arg;
164 	return ahd_intr(ahd);
165 }
166 
167 /*
168  * We have an scb which has been processed by the
169  * adaptor, now we look to see how the operation
170  * went.
171  */
172 void
173 ahd_done(struct ahd_softc *ahd, struct scb *scb)
174 {
175 	struct scsi_xfer *xs = scb->xs;
176 	int s;
177 
178 	/* XXX in ahc there is some bus_dmamap_sync(PREREAD|PREWRITE); */
179 
180 	LIST_REMOVE(scb, pending_links);
181 
182 	timeout_del(&xs->stimeout);
183 
184 	if (xs->datalen) {
185 		int op;
186 
187 		if ((xs->flags & SCSI_DATA_IN) != 0)
188 			op = BUS_DMASYNC_POSTREAD;
189 		else
190 			op = BUS_DMASYNC_POSTWRITE;
191 		bus_dmamap_sync(ahd->parent_dmat, scb->dmamap, 0,
192 		    scb->dmamap->dm_mapsize, op);
193 		bus_dmamap_unload(ahd->parent_dmat, scb->dmamap);
194 	}
195 
196 	/* Translate the CAM status code to a SCSI error code. */
197 	switch (xs->error) {
198 	case CAM_SCSI_STATUS_ERROR:
199 	case CAM_REQ_INPROG:
200 	case CAM_REQ_CMP:
201 		switch (xs->status) {
202 		case SCSI_TASKSET_FULL:
203 			/* SCSI Layer won't requeue, so we force infinite
204 			 * retries until queue space is available. XS_BUSY
205 			 * is dangerous because if the NOSLEEP flag is set
206 			 * it can cause the I/O to return EIO. XS_BUSY code
207 			 * falls through to XS_TIMEOUT anyway.
208 			 */
209 			xs->error = XS_TIMEOUT;
210 			xs->retries++;
211 			break;
212 		case SCSI_BUSY:
213 			xs->error = XS_BUSY;
214 			break;
215 		case SCSI_CHECK:
216 		case SCSI_TERMINATED:
217 			if ((scb->flags & SCB_SENSE) == 0) {
218 				/* CHECK on CHECK? */
219 				xs->error = XS_DRIVER_STUFFUP;
220 			} else
221 				xs->error = XS_NOERROR;
222 			break;
223 		default:
224 			xs->error = XS_NOERROR;
225 			break;
226 		}
227 		break;
228 	case CAM_BUSY:
229 		xs->error = XS_BUSY;
230 		break;
231 	case CAM_CMD_TIMEOUT:
232 		xs->error = XS_TIMEOUT;
233 		break;
234 	case CAM_BDR_SENT:
235 	case CAM_SCSI_BUS_RESET:
236 		xs->error = XS_RESET;
237 		break;
238 	case CAM_REQUEUE_REQ:
239 		xs->error = XS_TIMEOUT;
240 		xs->retries++;
241 		break;
242 	case CAM_SEL_TIMEOUT:
243 		xs->error = XS_SELTIMEOUT;
244 		break;
245 	default:
246 		xs->error = XS_DRIVER_STUFFUP;
247 		break;
248 	}
249 
250 	if (xs->error != XS_NOERROR) {
251 		/* Don't clobber any existing error state */
252 	} else if ((scb->flags & SCB_SENSE) != 0) {
253 		/*
254 		 * We performed autosense retrieval.
255 		 *
256 		 * Zero any sense not transferred by the
257 		 * device.  The SCSI spec mandates that any
258 		 * untransfered data should be assumed to be
259 		 * zero.  Complete the 'bounce' of sense information
260 		 * through buffers accessible via bus-space by
261 		 * copying it into the clients csio.
262 		 */
263 		memset(&xs->sense, 0, sizeof(struct scsi_sense_data));
264 		memcpy(&xs->sense, ahd_get_sense_buf(ahd, scb),
265 		    sizeof(struct scsi_sense_data));
266 		xs->error = XS_SENSE;
267 	} else if ((scb->flags & SCB_PKT_SENSE) != 0) {
268 		struct scsi_status_iu_header *siu;
269 		u_int32_t len;
270 
271  		siu = (struct scsi_status_iu_header *)scb->sense_data;
272 		len = SIU_SENSE_LENGTH(siu);
273 		memset(&xs->sense, 0, sizeof(xs->sense));
274 		memcpy(&xs->sense, SIU_SENSE_DATA(siu),
275 		    ulmin(len, sizeof(xs->sense)));
276 		xs->error = XS_SENSE;
277 	}
278 
279 	ahd_lock(ahd, &s);
280 	ahd_free_scb(ahd, scb);
281 	xs->flags |= ITSDONE;
282 	scsi_done(xs);
283 	ahd_unlock(ahd, &s);
284 }
285 
286 void
287 ahd_minphys(struct buf *bp, struct scsi_link *sl)
288 {
289 	/*
290 	 * Even though the card can transfer up to 16megs per command
291 	 * we are limited by the number of segments in the dma segment
292 	 * list that we can hold.  The worst case is that all pages are
293 	 * discontinuous physically, hence the "page per segment" limit
294 	 * enforced here.
295 	 */
296 	if (bp->b_bcount > ((AHD_NSEG - 1) * PAGE_SIZE)) {
297 		bp->b_bcount = ((AHD_NSEG - 1) * PAGE_SIZE);
298 	}
299 	minphys(bp);
300 }
301 
302 int32_t
303 ahd_action(struct scsi_xfer *xs)
304 {
305 	struct	ahd_softc *ahd;
306 	struct scb *scb;
307 	struct hardware_scb *hscb;
308 	u_int	target_id;
309 	u_int	our_id;
310 	int	s;
311 	struct	ahd_initiator_tinfo *tinfo;
312 	struct	ahd_tmode_tstate *tstate;
313 	u_int	col_idx;
314 	u_int16_t quirks;
315 
316 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("ahd_action\n"));
317 	ahd = (struct ahd_softc *)xs->sc_link->adapter_softc;
318 
319 	target_id = xs->sc_link->target;
320 	our_id = SCSI_SCSI_ID(ahd, xs->sc_link);
321 
322 	ahd_lock(ahd, &s);
323 	if ((ahd->flags & AHD_INITIATORROLE) == 0) {
324 		xs->error = XS_DRIVER_STUFFUP;
325 		xs->flags |= ITSDONE;
326 		scsi_done(xs);
327 		ahd_unlock(ahd, &s);
328 		return (COMPLETE);
329 	}
330 	/*
331 	 * get an scb to use.
332 	 */
333 	tinfo = ahd_fetch_transinfo(ahd, 'A', our_id, target_id, &tstate);
334 
335 	quirks = xs->sc_link->quirks;
336 
337 	if ((quirks & SDEV_NOTAGS) != 0 ||
338 	    (tinfo->curr.ppr_options & MSG_EXT_PPR_PROT_IUS) != 0)
339 		col_idx = AHD_NEVER_COL_IDX;
340 	else
341 		col_idx = AHD_BUILD_COL_IDX(target_id, xs->sc_link->lun);
342 
343 	if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
344 		ahd->flags |= AHD_RESOURCE_SHORTAGE;
345 		ahd_unlock(ahd, &s);
346 		return (NO_CCB);
347 	}
348 	ahd_unlock(ahd, &s);
349 
350 	hscb = scb->hscb;
351 
352 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("start scb(%p)\n", scb));
353 
354 	scb->xs = xs;
355 	timeout_set(&xs->stimeout, ahd_timeout, scb);
356 
357 	/*
358 	 * Put all the arguments for the xfer in the scb
359 	 */
360 	hscb->control = 0;
361 	hscb->scsiid = BUILD_SCSIID(ahd, xs->sc_link, target_id, our_id);
362 	hscb->lun = xs->sc_link->lun;
363 	if (xs->xs_control & XS_CTL_RESET) {
364 		hscb->cdb_len = 0;
365 		scb->flags |= SCB_DEVICE_RESET;
366 		hscb->control |= MK_MESSAGE;
367 		hscb->task_management = SIU_TASKMGMT_LUN_RESET;
368 		return (ahd_execute_scb(scb, NULL, 0));
369 	} else {
370 		hscb->task_management = 0;
371 		return (ahd_setup_data(ahd, xs, scb));
372 	}
373 }
374 
375 int
376 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
377 {
378 	struct	scb *scb;
379 	struct	scsi_xfer *xs;
380 	struct	ahd_softc *ahd;
381 	struct	ahd_initiator_tinfo *tinfo;
382 	struct	ahd_tmode_tstate *tstate;
383 	u_int	mask;
384 	int	s;
385 
386 	scb = (struct scb *)arg;
387 	xs = scb->xs;
388 	xs->error = CAM_REQ_INPROG;
389 	xs->status = 0;
390 	ahd = (struct ahd_softc *)xs->sc_link->adapter_softc;
391 
392 	if (nsegments != 0) {
393 		void *sg;
394 		int op;
395 		u_int i;
396 
397 		ahd_setup_data_scb(ahd, scb);
398 
399 		/* Copy the segments into our SG list */
400 		for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
401 
402 			sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
403 					  dm_segs->ds_len,
404 					  /*last*/i == 1);
405 			dm_segs++;
406 		}
407 
408 		if ((xs->flags & SCSI_DATA_IN) != 0)
409 			op = BUS_DMASYNC_PREREAD;
410 		else
411 			op = BUS_DMASYNC_PREWRITE;
412 
413 		bus_dmamap_sync(ahd->parent_dmat, scb->dmamap, 0,
414 				scb->dmamap->dm_mapsize, op);
415 
416 	}
417 
418 	ahd_lock(ahd, &s);
419 
420 	/*
421 	 * Last time we need to check if this SCB needs to
422 	 * be aborted.
423 	 */
424 	if (xs->flags & ITSDONE) {
425 		if (nsegments != 0)
426 			bus_dmamap_unload(ahd->parent_dmat,
427 					  scb->dmamap);
428 		ahd_free_scb(ahd, scb);
429 		ahd_unlock(ahd, &s);
430 		return (COMPLETE);
431 	}
432 
433 	tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
434 				    SCSIID_OUR_ID(scb->hscb->scsiid),
435 				    SCSIID_TARGET(ahd, scb->hscb->scsiid),
436 				    &tstate);
437 
438 	mask = SCB_GET_TARGET_MASK(ahd, scb);
439 
440 	if ((tstate->discenable & mask) != 0)
441 		scb->hscb->control |= DISCENB;
442 
443 	if ((tstate->tagenable & mask) != 0)
444 		scb->hscb->control |= TAG_ENB;
445 
446 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_PROT_IUS) != 0) {
447 		scb->flags |= SCB_PACKETIZED;
448 		if (scb->hscb->task_management != 0)
449 			scb->hscb->control &= ~MK_MESSAGE;
450 	}
451 
452 	if ((tstate->auto_negotiate & mask) != 0) {
453 		scb->flags |= SCB_AUTO_NEGOTIATE;
454 		scb->hscb->control |= MK_MESSAGE;
455 	}
456 
457 	/* XXX with ahc there was some bus_dmamap_sync(PREREAD|PREWRITE); */
458 
459 	LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
460 
461 	if (!(xs->flags & SCSI_POLL))
462 		timeout_add_msec(&xs->stimeout, xs->timeout);
463 
464 	scb->flags |= SCB_ACTIVE;
465 
466 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
467 		/* Define a mapping from our tag to the SCB. */
468 		ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
469 		ahd_pause(ahd);
470 		ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
471 		ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
472 		ahd_unpause(ahd);
473 	} else {
474 		ahd_queue_scb(ahd, scb);
475 	}
476 
477 	if (!(xs->flags & SCSI_POLL)) {
478 		int target = xs->sc_link->target;
479 		int lun = SCB_GET_LUN(scb);
480 
481 		if (ahd->inited_target[target] == 0) {
482 			struct  ahd_devinfo devinfo;
483 
484 			ahd_adapter_req_set_xfer_mode(ahd, scb);
485 			ahd_compile_devinfo(&devinfo, ahd->our_id, target, lun,
486 			    'A', /*XXX milos*/ROLE_UNKNOWN);
487 			ahd_scb_devinfo(ahd, &devinfo, scb);
488 			ahd_update_neg_request(ahd, &devinfo, tstate, tinfo,
489 				AHD_NEG_IF_NON_ASYNC);
490 			ahd->inited_target[target] = 1;
491 		}
492 
493 		ahd_unlock(ahd, &s);
494 		return (SUCCESSFULLY_QUEUED);
495 	}
496 
497 	/*
498 	 * If we can't use interrupts, poll for completion
499 	 */
500 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd_poll\n"));
501 
502 	do {
503 		if (ahd_poll(ahd, xs->timeout)) {
504 			if (!(xs->flags & SCSI_SILENT))
505 				printf("cmd fail\n");
506 			ahd_timeout(scb);
507 			break;
508 		}
509 	} while (!(xs->flags & ITSDONE));
510 
511 	ahd_unlock(ahd, &s);
512 	return (COMPLETE);
513 }
514 
515 int
516 ahd_poll(struct ahd_softc *ahd, int wait)
517 {
518 	while (--wait) {
519 		DELAY(1000);
520 		if (ahd_inb(ahd, INTSTAT) & INT_PEND)
521 			break;
522 	}
523 
524 	if (wait == 0) {
525 		printf("%s: board is not responding\n", ahd_name(ahd));
526 		return (EIO);
527 	}
528 
529 	ahd_intr((void *)ahd);
530 	return (0);
531 }
532 
533 int
534 ahd_setup_data(struct ahd_softc *ahd, struct scsi_xfer *xs,
535 	       struct scb *scb)
536 {
537 	struct hardware_scb *hscb;
538 	int s;
539 
540 	hscb = scb->hscb;
541 	xs->resid = xs->status = 0;
542 	xs->error = CAM_REQ_INPROG;
543 
544 	hscb->cdb_len = xs->cmdlen;
545 	if (hscb->cdb_len > MAX_CDB_LEN) {
546 		ahd_lock(ahd, &s);
547 		ahd_free_scb(ahd, scb);
548 		xs->error = XS_DRIVER_STUFFUP;
549 		xs->flags |= ITSDONE;
550 		scsi_done(xs);
551 		ahd_unlock(ahd, &s);
552 		return (COMPLETE);
553 	}
554 
555 	memcpy(hscb->shared_data.idata.cdb, xs->cmd, hscb->cdb_len);
556 
557 	/* Only use S/G if there is a transfer */
558 	if (xs->datalen) {
559 		int error;
560 
561 		error = bus_dmamap_load(ahd->parent_dmat,
562 					scb->dmamap, xs->data,
563 					xs->datalen, NULL,
564 					((xs->flags & SCSI_NOSLEEP) ?
565 					BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
566 					BUS_DMA_STREAMING |
567 					((xs->flags & XS_CTL_DATA_IN) ?
568 					BUS_DMA_READ : BUS_DMA_WRITE));
569 		if (error) {
570 #ifdef AHD_DEBUG
571 			printf("%s: in ahd_setup_data(): bus_dmamap_load() "
572 			    "= %d\n", ahd_name(ahd), error);
573 #endif
574 			ahd_lock(ahd, &s);
575 			ahd_free_scb(ahd, scb);
576 			ahd_unlock(ahd, &s);
577 			return (TRY_AGAIN_LATER);       /* XXX fvdl */
578 		}
579 		error = ahd_execute_scb(scb, scb->dmamap->dm_segs,
580 		    scb->dmamap->dm_nsegs);
581 		return error;
582 	} else {
583 		return ahd_execute_scb(scb, NULL, 0);
584 	}
585 }
586 
587 void
588 ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
589     ahd_queue_alg alg)
590 {
591 	struct ahd_tmode_tstate *tstate;
592 
593 	ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
594 	    devinfo->target, &tstate);
595 
596 	if (alg != AHD_QUEUE_NONE)
597 		tstate->tagenable |= devinfo->target_mask;
598 	else
599 		tstate->tagenable &= ~devinfo->target_mask;
600 }
601 
602 int
603 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
604 {
605 	if (sizeof(struct ahd_platform_data) > 0) {
606 		ahd->platform_data = malloc(sizeof(struct ahd_platform_data),
607 		    M_DEVBUF, M_NOWAIT | M_ZERO);
608 		if (ahd->platform_data == NULL)
609 			return (ENOMEM);
610 	}
611 
612 	return (0);
613 }
614 
615 void
616 ahd_platform_free(struct ahd_softc *ahd)
617 {
618 	if (sizeof(struct ahd_platform_data) > 0)
619 		free(ahd->platform_data, M_DEVBUF);
620 }
621 
622 int
623 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
624 {
625 	/* We don't sort softcs under OpenBSD so report equal always */
626 	return (0);
627 }
628 
629 int
630 ahd_detach(struct device *self, int flags)
631 {
632 	int rv = 0;
633 
634 	struct ahd_softc *ahd = (struct ahd_softc*)self;
635 
636 	if (ahd->sc_child != NULL)
637 		rv = config_detach((void *)ahd->sc_child, flags);
638 
639 	if (ahd->shutdown_hook != NULL)
640 		shutdownhook_disestablish(ahd->shutdown_hook);
641 
642 	ahd_free(ahd);
643 
644 	return rv;
645 }
646 
647 void
648 ahd_adapter_req_set_xfer_mode(struct ahd_softc *ahd, struct scb *scb)
649 {
650 	struct ahd_initiator_tinfo *tinfo;
651 	struct ahd_tmode_tstate *tstate;
652 	int target_id, our_id;
653 	struct ahd_devinfo devinfo;
654 	u_int16_t quirks;
655 	u_int width, ppr_options, period, offset;
656 	int s;
657 
658 	target_id = scb->xs->sc_link->target;
659 	our_id = SCSI_SCSI_ID(ahd, scb->xs->sc_link);
660 
661 	s = splbio();
662 
663 	quirks = scb->xs->sc_link->quirks;
664 	tinfo = ahd_fetch_transinfo(ahd, 'A', our_id, target_id, &tstate);
665 	ahd_compile_devinfo(&devinfo, our_id, target_id, 0, 'A',
666 	    ROLE_INITIATOR);
667 
668 	tstate->discenable |= (ahd->user_discenable & devinfo.target_mask);
669 
670 	if (quirks & SDEV_NOTAGS)
671 		tstate->tagenable &= ~devinfo.target_mask;
672 	else if (ahd->user_tagenable & devinfo.target_mask)
673 		tstate->tagenable |= devinfo.target_mask;
674 
675 	if (quirks & SDEV_NOWIDE)
676 		width = MSG_EXT_WDTR_BUS_8_BIT;
677 	else
678 		width = MSG_EXT_WDTR_BUS_16_BIT;
679 
680 	ahd_validate_width(ahd, NULL, &width, ROLE_UNKNOWN);
681 	if (width > tinfo->user.width)
682 		width = tinfo->user.width;
683 	ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
684 
685 	if (quirks & SDEV_NOSYNC) {
686 		period = 0;
687 		offset = 0;
688 	} else {
689 		period = tinfo->user.period;
690 		offset = tinfo->user.offset;
691 	}
692 
693 	/* XXX Look at saved INQUIRY flags for PPR capabilities XXX */
694 	ppr_options = tinfo->user.ppr_options;
695 	/* XXX Other reasons to avoid ppr? XXX */
696 	if (width < MSG_EXT_WDTR_BUS_16_BIT)
697 		ppr_options = 0;
698 
699 	if ((tstate->discenable & devinfo.target_mask) == 0 ||
700 	    (tstate->tagenable & devinfo.target_mask) == 0)
701 		ppr_options &= ~MSG_EXT_PPR_PROT_IUS;
702 
703 	ahd_find_syncrate(ahd, &period, &ppr_options, AHD_SYNCRATE_MAX);
704 	ahd_validate_offset(ahd, NULL, period, &offset, width, ROLE_UNKNOWN);
705 
706 	if (offset == 0) {
707 		period = 0;
708 		ppr_options = 0;
709 	}
710 
711 	if (ppr_options != 0 && tinfo->user.transport_version >= 3) {
712 		tinfo->goal.transport_version = tinfo->user.transport_version;
713 		tinfo->curr.transport_version = tinfo->user.transport_version;
714 	}
715 
716 	ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
717 		AHD_TRANS_GOAL, FALSE);
718 
719 	splx(s);
720 }
721 
722 void
723 aic_timer_reset(aic_timer_t *timer, u_int msec, ahd_callback_t *func,
724     void *arg)
725 {
726 	uint64_t ticks;
727 
728 	ticks = msec;
729 	ticks *= hz;
730 	ticks /= 1000;
731 	callout_reset(timer, ticks, func, arg);
732 }
733 
734 void
735 aic_scb_timer_reset(struct scb *scb, u_int msec)
736 {
737 	uint64_t ticks;
738 
739 	ticks = msec;
740 	ticks *= hz;
741 	ticks /= 1000;
742 	if (!(scb->xs->xs_control & XS_CTL_POLL))
743 		callout_reset(&scb->xs->xs_callout, ticks, ahd_timeout, scb);
744 }
745 
746 void
747 ahd_flush_device_writes(struct ahd_softc *ahd)
748 {
749 	/* XXX Is this sufficient for all architectures??? */
750 	ahd_inb(ahd, INTSTAT);
751 }
752 
753 void
754 aic_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
755 {
756 	int s;
757 
758 	ahd_lock(ahd, &s);
759 
760 	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0) {
761 		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
762 	}
763 
764 	if (!cold) {
765 		/* we are no longer in autoconf */
766 		timeout_del(&scb->xs->stimeout);
767 	}
768 
769 	ahd_unlock(ahd, &s);
770 }
771 
772 void
773 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
774 {
775 	sc_print_addr(scb->xs->sc_link);
776 }
777 
778 void
779 ahd_platform_dump_card_state(struct ahd_softc *ahd)
780 {
781 	/* Nothing to do here for OpenBSD */
782 	printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
783 		ahd->features, ahd->flags, ahd->chip, ahd->bugs);
784 }
785 
786 void
787 ahd_platform_flushwork(struct ahd_softc *ahd)
788 {
789 }
790