xref: /freebsd/sys/dev/aic7xxx/aic_osm_lib.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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 __FBSDID("$FreeBSD$");
37 
38 static void	aic_recovery_thread(void *arg);
39 
40 void
41 aic_set_recoveryscb(struct aic_softc *aic, struct scb *scb)
42 {
43 
44 	if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
45 		struct scb *list_scb;
46 
47 		scb->flags |= SCB_RECOVERY_SCB;
48 
49 		AIC_SCB_DATA(aic)->recovery_scbs++;
50 
51 		/*
52 		 * Go through all of our pending SCBs and remove
53 		 * any scheduled timeouts for them.  We will reschedule
54 		 * them after we've successfully fixed this problem.
55 		 */
56 		LIST_FOREACH(list_scb, &aic->pending_scbs, pending_links) {
57 			union ccb *ccb;
58 
59 			ccb = list_scb->io_ctx;
60 			untimeout(aic_platform_timeout, list_scb,
61 				  ccb->ccb_h.timeout_ch);
62 		}
63 	}
64 }
65 
66 void
67 aic_platform_timeout(void *arg)
68 {
69 	struct	scb *scb;
70 	u_long	s;
71 
72 	scb = (struct scb *)arg;
73 	aic_lock(scb->aic_softc, &s);
74 	aic_timeout(scb);
75 	aic_unlock(scb->aic_softc, &s);
76 }
77 
78 int
79 aic_spawn_recovery_thread(struct aic_softc *aic)
80 {
81 	int error;
82 
83 	error = aic_kthread_create(aic_recovery_thread, aic,
84 			       &aic->platform_data->recovery_thread,
85 			       /*flags*/0, /*altstack*/0, "aic_recovery%d",
86 			       aic->unit);
87 	return (error);
88 }
89 
90 /*
91  * Lock is not held on entry.
92  */
93 void
94 aic_terminate_recovery_thread(struct aic_softc *aic)
95 {
96 	u_long s;
97 
98 	aic_lock(aic, &s);
99 	if (aic->platform_data->recovery_thread == NULL) {
100 		aic_unlock(aic, &s);
101 		return;
102 	}
103 	aic->flags |= AIC_SHUTDOWN_RECOVERY;
104 	wakeup(aic);
105 	/*
106 	 * Sleep on a slightly different location
107 	 * for this interlock just for added safety.
108 	 */
109 	tsleep(aic->platform_data, PUSER, "thtrm", 0);
110 	aic_unlock(aic, &s);
111 }
112 
113 static void
114 aic_recovery_thread(void *arg)
115 {
116 	struct aic_softc *aic;
117 	u_long s;
118 
119 #if __FreeBSD_version >= 500000
120 	mtx_lock(&Giant);
121 #endif
122 	aic = (struct aic_softc *)arg;
123 	aic_lock(aic, &s);
124 	for (;;) {
125 
126 		if (LIST_EMPTY(&aic->timedout_scbs) != 0
127 		 && (aic->flags & AIC_SHUTDOWN_RECOVERY) == 0)
128 			tsleep(aic, PUSER, "idle", 0);
129 
130 		if ((aic->flags & AIC_SHUTDOWN_RECOVERY) != 0)
131 			break;
132 
133 		aic_unlock(aic, &s);
134 		aic_recover_commands(aic);
135 		aic_lock(aic, &s);
136 	}
137 	aic->platform_data->recovery_thread = NULL;
138 	wakeup(aic->platform_data);
139 	aic_unlock(aic, &s);
140 #if __FreeBSD_version >= 500000
141 	mtx_unlock(&Giant);
142 #endif
143 	kthread_exit(0);
144 }
145 
146 void
147 aic_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
148 {
149 #if __FreeBSD_version >= 500000
150 	cam_calc_geometry(ccg, extended);
151 #else
152 	uint32_t size_mb;
153 	uint32_t secs_per_cylinder;
154 
155 	size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
156 	if (size_mb > 1024 && extended) {
157 		ccg->heads = 255;
158 		ccg->secs_per_track = 63;
159 	} else {
160 		ccg->heads = 64;
161 		ccg->secs_per_track = 32;
162 	}
163 	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
164 	ccg->cylinders = ccg->volume_size / secs_per_cylinder;
165 	ccg->ccb_h.status = CAM_REQ_CMP;
166 #endif
167 }
168 
169