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