xref: /freebsd/sys/dev/aic7xxx/aic_osm_lib.c (revision 685dc743)
1098ca2bdSWarner Losh /*-
2b3b25f2cSJustin T. Gibbs  * FreeBSD OSM Library for the aic7xxx aic79xx based Adaptec SCSI controllers
3b3b25f2cSJustin T. Gibbs  *
4b3b25f2cSJustin T. Gibbs  * Copyright (c) 1994-2002 Justin T. Gibbs.
5b3b25f2cSJustin T. Gibbs  * Copyright (c) 2001-2003 Adaptec Inc.
6b3b25f2cSJustin T. Gibbs  * All rights reserved.
7b3b25f2cSJustin T. Gibbs  *
8b3b25f2cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
9b3b25f2cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
10b3b25f2cSJustin T. Gibbs  * are met:
11b3b25f2cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
12b3b25f2cSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
13b3b25f2cSJustin T. Gibbs  *    without modification.
14b3b25f2cSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
15b3b25f2cSJustin T. Gibbs  *    derived from this software without specific prior written permission.
16b3b25f2cSJustin T. Gibbs  *
17b3b25f2cSJustin T. Gibbs  * Alternatively, this software may be distributed under the terms of the
18b3b25f2cSJustin T. Gibbs  * GNU Public License ("GPL").
19b3b25f2cSJustin T. Gibbs  *
20b3b25f2cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21b3b25f2cSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22b3b25f2cSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23b3b25f2cSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24b3b25f2cSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25b3b25f2cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26b3b25f2cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27b3b25f2cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28b3b25f2cSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29b3b25f2cSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30b3b25f2cSJustin T. Gibbs  * SUCH DAMAGE.
31b3b25f2cSJustin T. Gibbs  *
32670c5599SJustin T. Gibbs  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic_osm_lib.c#5 $
33b3b25f2cSJustin T. Gibbs  */
34b3b25f2cSJustin T. Gibbs 
35b3b25f2cSJustin T. Gibbs #include <sys/cdefs.h>
36b3b25f2cSJustin T. Gibbs static void	aic_recovery_thread(void *arg);
37b3b25f2cSJustin T. Gibbs 
38b3b25f2cSJustin T. Gibbs void
aic_set_recoveryscb(struct aic_softc * aic,struct scb * scb)39b3b25f2cSJustin T. Gibbs aic_set_recoveryscb(struct aic_softc *aic, struct scb *scb)
40b3b25f2cSJustin T. Gibbs {
41b3b25f2cSJustin T. Gibbs 
42b3b25f2cSJustin T. Gibbs 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
43b3b25f2cSJustin T. Gibbs 		struct scb *list_scb;
44b3b25f2cSJustin T. Gibbs 
45b3b25f2cSJustin T. Gibbs 		scb->flags |= SCB_RECOVERY_SCB;
46b3b25f2cSJustin T. Gibbs 
477afc0218SJustin T. Gibbs 		AIC_SCB_DATA(aic)->recovery_scbs++;
48b3b25f2cSJustin T. Gibbs 
49b3b25f2cSJustin T. Gibbs 		/*
50b3b25f2cSJustin T. Gibbs 		 * Go through all of our pending SCBs and remove
51b3b25f2cSJustin T. Gibbs 		 * any scheduled timeouts for them.  We will reschedule
52b3b25f2cSJustin T. Gibbs 		 * them after we've successfully fixed this problem.
53b3b25f2cSJustin T. Gibbs 		 */
54b3b25f2cSJustin T. Gibbs 		LIST_FOREACH(list_scb, &aic->pending_scbs, pending_links) {
55032b0a17SScott Long 			callout_stop(&scb->io_timer);
56b3b25f2cSJustin T. Gibbs 		}
57b3b25f2cSJustin T. Gibbs 	}
58b3b25f2cSJustin T. Gibbs }
59b3b25f2cSJustin T. Gibbs 
60b3b25f2cSJustin T. Gibbs void
aic_platform_timeout(void * arg)61b3b25f2cSJustin T. Gibbs aic_platform_timeout(void *arg)
62b3b25f2cSJustin T. Gibbs {
63b3b25f2cSJustin T. Gibbs 	struct	scb *scb;
64b3b25f2cSJustin T. Gibbs 
65b3b25f2cSJustin T. Gibbs 	scb = (struct scb *)arg;
66032b0a17SScott Long 	aic_lock(scb->aic_softc);
67b3b25f2cSJustin T. Gibbs 	aic_timeout(scb);
68032b0a17SScott Long 	aic_unlock(scb->aic_softc);
69b3b25f2cSJustin T. Gibbs }
70b3b25f2cSJustin T. Gibbs 
71b3b25f2cSJustin T. Gibbs int
aic_spawn_recovery_thread(struct aic_softc * aic)72b3b25f2cSJustin T. Gibbs aic_spawn_recovery_thread(struct aic_softc *aic)
73b3b25f2cSJustin T. Gibbs {
74b3b25f2cSJustin T. Gibbs 	int error;
75b3b25f2cSJustin T. Gibbs 
76b3b25f2cSJustin T. Gibbs 	error = aic_kthread_create(aic_recovery_thread, aic,
77b3b25f2cSJustin T. Gibbs 			       &aic->platform_data->recovery_thread,
78b3b25f2cSJustin T. Gibbs 			       /*flags*/0, /*altstack*/0, "aic_recovery%d",
79b3b25f2cSJustin T. Gibbs 			       aic->unit);
80b3b25f2cSJustin T. Gibbs 	return (error);
81b3b25f2cSJustin T. Gibbs }
82b3b25f2cSJustin T. Gibbs 
83b3b25f2cSJustin T. Gibbs /*
84b3b25f2cSJustin T. Gibbs  * Lock is not held on entry.
85b3b25f2cSJustin T. Gibbs  */
86b3b25f2cSJustin T. Gibbs void
aic_terminate_recovery_thread(struct aic_softc * aic)87b3b25f2cSJustin T. Gibbs aic_terminate_recovery_thread(struct aic_softc *aic)
88b3b25f2cSJustin T. Gibbs {
89b3b25f2cSJustin T. Gibbs 
90b3b25f2cSJustin T. Gibbs 	if (aic->platform_data->recovery_thread == NULL) {
91b3b25f2cSJustin T. Gibbs 		return;
92b3b25f2cSJustin T. Gibbs 	}
93b3b25f2cSJustin T. Gibbs 	aic->flags |= AIC_SHUTDOWN_RECOVERY;
94b3b25f2cSJustin T. Gibbs 	wakeup(aic);
95b3b25f2cSJustin T. Gibbs 	/*
96b3b25f2cSJustin T. Gibbs 	 * Sleep on a slightly different location
97b3b25f2cSJustin T. Gibbs 	 * for this interlock just for added safety.
98b3b25f2cSJustin T. Gibbs 	 */
99032b0a17SScott Long 	msleep(aic->platform_data, &aic->platform_data->mtx, PUSER, "thtrm", 0);
100b3b25f2cSJustin T. Gibbs }
101b3b25f2cSJustin T. Gibbs 
102670c5599SJustin T. Gibbs static void
aic_recovery_thread(void * arg)103670c5599SJustin T. Gibbs aic_recovery_thread(void *arg)
104670c5599SJustin T. Gibbs {
105670c5599SJustin T. Gibbs 	struct aic_softc *aic;
106670c5599SJustin T. Gibbs 
107670c5599SJustin T. Gibbs 	aic = (struct aic_softc *)arg;
108032b0a17SScott Long 	aic_lock(aic);
109670c5599SJustin T. Gibbs 	for (;;) {
110670c5599SJustin T. Gibbs 
111670c5599SJustin T. Gibbs 		if (LIST_EMPTY(&aic->timedout_scbs) != 0
112670c5599SJustin T. Gibbs 		 && (aic->flags & AIC_SHUTDOWN_RECOVERY) == 0)
113032b0a17SScott Long 			msleep(aic, &aic->platform_data->mtx, PUSER, "idle", 0);
114670c5599SJustin T. Gibbs 
115670c5599SJustin T. Gibbs 		if ((aic->flags & AIC_SHUTDOWN_RECOVERY) != 0)
116670c5599SJustin T. Gibbs 			break;
117670c5599SJustin T. Gibbs 
118670c5599SJustin T. Gibbs 		aic_recover_commands(aic);
119670c5599SJustin T. Gibbs 	}
120670c5599SJustin T. Gibbs 	aic->platform_data->recovery_thread = NULL;
121670c5599SJustin T. Gibbs 	wakeup(aic->platform_data);
122032b0a17SScott Long 	aic_unlock(aic);
1233745c395SJulian Elischer 	kproc_exit(0);
124670c5599SJustin T. Gibbs }
125670c5599SJustin T. Gibbs 
126b3b25f2cSJustin T. Gibbs void
aic_calc_geometry(struct ccb_calc_geometry * ccg,int extended)127b3b25f2cSJustin T. Gibbs aic_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
128b3b25f2cSJustin T. Gibbs {
129b3b25f2cSJustin T. Gibbs 	cam_calc_geometry(ccg, extended);
130b3b25f2cSJustin T. Gibbs }
131