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