twa.c revision 1.9.2.2 1 1.9.2.2 tron /* $NetBSD: twa.c,v 1.9.2.2 2006/07/31 12:35:23 tron Exp $ */
2 1.9.2.2 tron /* $wasabi: twa.c,v 1.27 2006/07/28 18:17:21 wrstuden Exp $ */
3 1.9.2.2 tron
4 1.9.2.2 tron /*-
5 1.9.2.2 tron * Copyright (c) 2004 The NetBSD Foundation, Inc.
6 1.9.2.2 tron * All rights reserved.
7 1.9.2.2 tron *
8 1.9.2.2 tron * This code is derived from software contributed to The NetBSD Foundation
9 1.9.2.2 tron * by Jordan Rhody of Wasabi Systems, Inc.
10 1.9.2.2 tron *
11 1.9.2.2 tron * Redistribution and use in source and binary forms, with or without
12 1.9.2.2 tron * modification, are permitted provided that the following conditions
13 1.9.2.2 tron * are met:
14 1.9.2.2 tron * 1. Redistributions of source code must retain the above copyright
15 1.9.2.2 tron * notice, this list of conditions and the following disclaimer.
16 1.9.2.2 tron * 2. Redistributions in binary form must reproduce the above copyright
17 1.9.2.2 tron * notice, this list of conditions and the following disclaimer in the
18 1.9.2.2 tron * documentation and/or other materials provided with the distribution.
19 1.9.2.2 tron * 3. All advertising materials mentioning features or use of this software
20 1.9.2.2 tron * must display the following acknowledgement:
21 1.9.2.2 tron * This product includes software developed by the NetBSD
22 1.9.2.2 tron * Foundation, Inc. and its contributors.
23 1.9.2.2 tron * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.9.2.2 tron * contributors may be used to endorse or promote products derived
25 1.9.2.2 tron * from this software without specific prior written permission.
26 1.9.2.2 tron *
27 1.9.2.2 tron * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.9.2.2 tron * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.9.2.2 tron * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.9.2.2 tron * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.9.2.2 tron * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.9.2.2 tron * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.9.2.2 tron * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.9.2.2 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.9.2.2 tron * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.9.2.2 tron * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.9.2.2 tron * POSSIBILITY OF SUCH DAMAGE.
38 1.9.2.2 tron */
39 1.9.2.2 tron
40 1.9.2.2 tron /*-
41 1.9.2.2 tron * Copyright (c) 2003-04 3ware, Inc.
42 1.9.2.2 tron * Copyright (c) 2000 Michael Smith
43 1.9.2.2 tron * Copyright (c) 2000 BSDi
44 1.9.2.2 tron * All rights reserved.
45 1.9.2.2 tron *
46 1.9.2.2 tron * Redistribution and use in source and binary forms, with or without
47 1.9.2.2 tron * modification, are permitted provided that the following conditions
48 1.9.2.2 tron * are met:
49 1.9.2.2 tron * 1. Redistributions of source code must retain the above copyright
50 1.9.2.2 tron * notice, this list of conditions and the following disclaimer.
51 1.9.2.2 tron * 2. Redistributions in binary form must reproduce the above copyright
52 1.9.2.2 tron * notice, this list of conditions and the following disclaimer in the
53 1.9.2.2 tron * documentation and/or other materials provided with the distribution.
54 1.9.2.2 tron *
55 1.9.2.2 tron * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
56 1.9.2.2 tron * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 1.9.2.2 tron * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 1.9.2.2 tron * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
59 1.9.2.2 tron * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 1.9.2.2 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 1.9.2.2 tron * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 1.9.2.2 tron * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 1.9.2.2 tron * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 1.9.2.2 tron * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 1.9.2.2 tron * SUCH DAMAGE.
66 1.9.2.2 tron *
67 1.9.2.2 tron * $FreeBSD: src/sys/dev/twa/twa.c,v 1.2 2004/04/02 15:09:57 des Exp $
68 1.9.2.2 tron */
69 1.9.2.2 tron
70 1.9.2.2 tron /*
71 1.9.2.2 tron * 3ware driver for 9000 series storage controllers.
72 1.9.2.2 tron *
73 1.9.2.2 tron * Author: Vinod Kashyap
74 1.9.2.2 tron */
75 1.9.2.2 tron
76 1.9.2.2 tron #include <sys/cdefs.h>
77 1.9.2.2 tron __KERNEL_RCSID(0, "$NetBSD: twa.c,v 1.9.2.2 2006/07/31 12:35:23 tron Exp $");
78 1.9.2.2 tron
79 1.9.2.2 tron #include <sys/param.h>
80 1.9.2.2 tron #include <sys/systm.h>
81 1.9.2.2 tron #include <sys/kernel.h>
82 1.9.2.2 tron #include <sys/device.h>
83 1.9.2.2 tron #include <sys/queue.h>
84 1.9.2.2 tron #include <sys/proc.h>
85 1.9.2.2 tron #include <sys/bswap.h>
86 1.9.2.2 tron #include <sys/buf.h>
87 1.9.2.2 tron #include <sys/bufq.h>
88 1.9.2.2 tron #include <sys/endian.h>
89 1.9.2.2 tron #include <sys/malloc.h>
90 1.9.2.2 tron #include <sys/conf.h>
91 1.9.2.2 tron #include <sys/disk.h>
92 1.9.2.2 tron #include <sys/syslog.h>
93 1.9.2.2 tron
94 1.9.2.2 tron #include <uvm/uvm_extern.h>
95 1.9.2.2 tron
96 1.9.2.2 tron #include <machine/bus.h>
97 1.9.2.2 tron
98 1.9.2.2 tron #include <dev/pci/pcireg.h>
99 1.9.2.2 tron #include <dev/pci/pcivar.h>
100 1.9.2.2 tron #include <dev/pci/pcidevs.h>
101 1.9.2.2 tron #include <dev/pci/twareg.h>
102 1.9.2.2 tron #include <dev/pci/twavar.h>
103 1.9.2.2 tron #include <dev/pci/twaio.h>
104 1.9.2.2 tron
105 1.9.2.2 tron #include <dev/scsipi/scsipi_all.h>
106 1.9.2.2 tron #include <dev/scsipi/scsipi_disk.h>
107 1.9.2.2 tron #include <dev/scsipi/scsipiconf.h>
108 1.9.2.2 tron #include <dev/scsipi/scsi_spc.h>
109 1.9.2.2 tron
110 1.9.2.2 tron #include <dev/ldvar.h>
111 1.9.2.2 tron
112 1.9.2.2 tron #include "locators.h"
113 1.9.2.2 tron
114 1.9.2.2 tron #define PCI_CBIO 0x10
115 1.9.2.2 tron
116 1.9.2.2 tron static int twa_fetch_aen(struct twa_softc *);
117 1.9.2.2 tron static void twa_aen_callback(struct twa_request *);
118 1.9.2.2 tron static int twa_find_aen(struct twa_softc *sc, uint16_t);
119 1.9.2.2 tron static uint16_t twa_enqueue_aen(struct twa_softc *sc,
120 1.9.2.2 tron struct twa_command_header *);
121 1.9.2.2 tron
122 1.9.2.2 tron static void twa_attach(struct device *, struct device *, void *);
123 1.9.2.2 tron static void twa_shutdown(void *);
124 1.9.2.2 tron static int twa_init_connection(struct twa_softc *, uint16_t, uint32_t,
125 1.9.2.2 tron uint16_t, uint16_t, uint16_t, uint16_t, uint16_t *,
126 1.9.2.2 tron uint16_t *, uint16_t *, uint16_t *, uint32_t *);
127 1.9.2.2 tron static int twa_intr(void *);
128 1.9.2.2 tron static int twa_match(struct device *, struct cfdata *, void *);
129 1.9.2.2 tron static int twa_submatch(struct device *, struct cfdata *,
130 1.9.2.2 tron const locdesc_t *, void *);
131 1.9.2.2 tron static int twa_reset(struct twa_softc *);
132 1.9.2.2 tron
133 1.9.2.2 tron static int twa_print(void *, const char *);
134 1.9.2.2 tron static int twa_soft_reset(struct twa_softc *);
135 1.9.2.2 tron
136 1.9.2.2 tron static int twa_check_ctlr_state(struct twa_softc *, uint32_t);
137 1.9.2.2 tron static int twa_get_param(struct twa_softc *, int, int, size_t,
138 1.9.2.2 tron void (* callback)(struct twa_request *),
139 1.9.2.2 tron struct twa_param_9k **);
140 1.9.2.2 tron static int twa_set_param(struct twa_softc *, int, int, int, void *,
141 1.9.2.2 tron void (* callback)(struct twa_request *));
142 1.9.2.2 tron static void twa_describe_controller(struct twa_softc *);
143 1.9.2.2 tron static int twa_wait_status(struct twa_softc *, uint32_t, uint32_t);
144 1.9.2.2 tron static int twa_done(struct twa_softc *);
145 1.9.2.2 tron #if 0
146 1.9.2.2 tron static int twa_flash_firmware(struct twa_softc *sc);
147 1.9.2.2 tron static int twa_hard_reset(struct twa_softc *sc);
148 1.9.2.2 tron #endif
149 1.9.2.2 tron
150 1.9.2.2 tron extern struct cfdriver twa_cd;
151 1.9.2.2 tron extern uint32_t twa_fw_img_size;
152 1.9.2.2 tron extern uint8_t twa_fw_img[];
153 1.9.2.2 tron
154 1.9.2.2 tron CFATTACH_DECL(twa, sizeof(struct twa_softc),
155 1.9.2.2 tron twa_match, twa_attach, NULL, NULL);
156 1.9.2.2 tron
157 1.9.2.2 tron /* AEN messages. */
158 1.9.2.2 tron static const struct twa_message twa_aen_table[] = {
159 1.9.2.2 tron {0x0000, "AEN queue empty"},
160 1.9.2.2 tron {0x0001, "Controller reset occurred"},
161 1.9.2.2 tron {0x0002, "Degraded unit detected"},
162 1.9.2.2 tron {0x0003, "Controller error occured"},
163 1.9.2.2 tron {0x0004, "Background rebuild failed"},
164 1.9.2.2 tron {0x0005, "Background rebuild done"},
165 1.9.2.2 tron {0x0006, "Incomplete unit detected"},
166 1.9.2.2 tron {0x0007, "Background initialize done"},
167 1.9.2.2 tron {0x0008, "Unclean shutdown detected"},
168 1.9.2.2 tron {0x0009, "Drive timeout detected"},
169 1.9.2.2 tron {0x000A, "Drive error detected"},
170 1.9.2.2 tron {0x000B, "Rebuild started"},
171 1.9.2.2 tron {0x000C, "Background initialize started"},
172 1.9.2.2 tron {0x000D, "Entire logical unit was deleted"},
173 1.9.2.2 tron {0x000E, "Background initialize failed"},
174 1.9.2.2 tron {0x000F, "SMART attribute exceeded threshold"},
175 1.9.2.2 tron {0x0010, "Power supply reported AC under range"},
176 1.9.2.2 tron {0x0011, "Power supply reported DC out of range"},
177 1.9.2.2 tron {0x0012, "Power supply reported a malfunction"},
178 1.9.2.2 tron {0x0013, "Power supply predicted malfunction"},
179 1.9.2.2 tron {0x0014, "Battery charge is below threshold"},
180 1.9.2.2 tron {0x0015, "Fan speed is below threshold"},
181 1.9.2.2 tron {0x0016, "Temperature sensor is above threshold"},
182 1.9.2.2 tron {0x0017, "Power supply was removed"},
183 1.9.2.2 tron {0x0018, "Power supply was inserted"},
184 1.9.2.2 tron {0x0019, "Drive was removed from a bay"},
185 1.9.2.2 tron {0x001A, "Drive was inserted into a bay"},
186 1.9.2.2 tron {0x001B, "Drive bay cover door was opened"},
187 1.9.2.2 tron {0x001C, "Drive bay cover door was closed"},
188 1.9.2.2 tron {0x001D, "Product case was opened"},
189 1.9.2.2 tron {0x0020, "Prepare for shutdown (power-off)"},
190 1.9.2.2 tron {0x0021, "Downgrade UDMA mode to lower speed"},
191 1.9.2.2 tron {0x0022, "Upgrade UDMA mode to higher speed"},
192 1.9.2.2 tron {0x0023, "Sector repair completed"},
193 1.9.2.2 tron {0x0024, "Sbuf memory test failed"},
194 1.9.2.2 tron {0x0025, "Error flushing cached write data to disk"},
195 1.9.2.2 tron {0x0026, "Drive reported data ECC error"},
196 1.9.2.2 tron {0x0027, "DCB has checksum error"},
197 1.9.2.2 tron {0x0028, "DCB version is unsupported"},
198 1.9.2.2 tron {0x0029, "Background verify started"},
199 1.9.2.2 tron {0x002A, "Background verify failed"},
200 1.9.2.2 tron {0x002B, "Background verify done"},
201 1.9.2.2 tron {0x002C, "Bad sector overwritten during rebuild"},
202 1.9.2.2 tron {0x002E, "Replace failed because replacement drive too small"},
203 1.9.2.2 tron {0x002F, "Verify failed because array was never initialized"},
204 1.9.2.2 tron {0x0030, "Unsupported ATA drive"},
205 1.9.2.2 tron {0x0031, "Synchronize host/controller time"},
206 1.9.2.2 tron {0x0032, "Spare capacity is inadequate for some units"},
207 1.9.2.2 tron {0x0033, "Background migration started"},
208 1.9.2.2 tron {0x0034, "Background migration failed"},
209 1.9.2.2 tron {0x0035, "Background migration done"},
210 1.9.2.2 tron {0x0036, "Verify detected and fixed data/parity mismatch"},
211 1.9.2.2 tron {0x0037, "SO-DIMM incompatible"},
212 1.9.2.2 tron {0x0038, "SO-DIMM not detected"},
213 1.9.2.2 tron {0x0039, "Corrected Sbuf ECC error"},
214 1.9.2.2 tron {0x003A, "Drive power on reset detected"},
215 1.9.2.2 tron {0x003B, "Background rebuild paused"},
216 1.9.2.2 tron {0x003C, "Background initialize paused"},
217 1.9.2.2 tron {0x003D, "Background verify paused"},
218 1.9.2.2 tron {0x003E, "Background migration paused"},
219 1.9.2.2 tron {0x003F, "Corrupt flash file system detected"},
220 1.9.2.2 tron {0x0040, "Flash file system repaired"},
221 1.9.2.2 tron {0x0041, "Unit number assignments were lost"},
222 1.9.2.2 tron {0x0042, "Error during read of primary DCB"},
223 1.9.2.2 tron {0x0043, "Latent error found in backup DCB"},
224 1.9.2.2 tron {0x0044, "Battery voltage is normal"},
225 1.9.2.2 tron {0x0045, "Battery voltage is low"},
226 1.9.2.2 tron {0x0046, "Battery voltage is high"},
227 1.9.2.2 tron {0x0047, "Battery voltage is too low"},
228 1.9.2.2 tron {0x0048, "Battery voltage is too high"},
229 1.9.2.2 tron {0x0049, "Battery temperature is normal"},
230 1.9.2.2 tron {0x004A, "Battery temperature is low"},
231 1.9.2.2 tron {0x004B, "Battery temperature is high"},
232 1.9.2.2 tron {0x004C, "Battery temperature is too low"},
233 1.9.2.2 tron {0x004D, "Battery temperature is too high"},
234 1.9.2.2 tron {0x004E, "Battery capacity test started"},
235 1.9.2.2 tron {0x004F, "Cache synchronization skipped"},
236 1.9.2.2 tron {0x0050, "Battery capacity test completed"},
237 1.9.2.2 tron {0x0051, "Battery health check started"},
238 1.9.2.2 tron {0x0052, "Battery health check completed"},
239 1.9.2.2 tron {0x0053, "Need to do a capacity test"},
240 1.9.2.2 tron {0x0054, "Charge termination voltage is at high level"},
241 1.9.2.2 tron {0x0055, "Battery charging started"},
242 1.9.2.2 tron {0x0056, "Battery charging completed"},
243 1.9.2.2 tron {0x0057, "Battery charging fault"},
244 1.9.2.2 tron {0x0058, "Battery capacity is below warning level"},
245 1.9.2.2 tron {0x0059, "Battery capacity is below error level"},
246 1.9.2.2 tron {0x005A, "Battery is present"},
247 1.9.2.2 tron {0x005B, "Battery is not present"},
248 1.9.2.2 tron {0x005C, "Battery is weak"},
249 1.9.2.2 tron {0x005D, "Battery health check failed"},
250 1.9.2.2 tron {0x005E, "Cache synchronized after power fail"},
251 1.9.2.2 tron {0x005F, "Cache synchronization failed; some data lost"},
252 1.9.2.2 tron {0x0060, "Bad cache meta data checksum"},
253 1.9.2.2 tron {0x0061, "Bad cache meta data signature"},
254 1.9.2.2 tron {0x0062, "Cache meta data restore failed"},
255 1.9.2.2 tron {0x0063, "BBU not found after power fail"},
256 1.9.2.2 tron {0x00FC, "Recovered/finished array membership update"},
257 1.9.2.2 tron {0x00FD, "Handler lockup"},
258 1.9.2.2 tron {0x00FE, "Retrying PCI transfer"},
259 1.9.2.2 tron {0x00FF, "AEN queue is full"},
260 1.9.2.2 tron {0xFFFFFFFF, (char *)NULL}
261 1.9.2.2 tron };
262 1.9.2.2 tron
263 1.9.2.2 tron /* AEN severity table. */
264 1.9.2.2 tron static const char *twa_aen_severity_table[] = {
265 1.9.2.2 tron "None",
266 1.9.2.2 tron "ERROR",
267 1.9.2.2 tron "WARNING",
268 1.9.2.2 tron "INFO",
269 1.9.2.2 tron "DEBUG",
270 1.9.2.2 tron (char *)NULL
271 1.9.2.2 tron };
272 1.9.2.2 tron
273 1.9.2.2 tron /* Error messages. */
274 1.9.2.2 tron static const struct twa_message twa_error_table[] = {
275 1.9.2.2 tron {0x0100, "SGL entry contains zero data"},
276 1.9.2.2 tron {0x0101, "Invalid command opcode"},
277 1.9.2.2 tron {0x0102, "SGL entry has unaligned address"},
278 1.9.2.2 tron {0x0103, "SGL size does not match command"},
279 1.9.2.2 tron {0x0104, "SGL entry has illegal length"},
280 1.9.2.2 tron {0x0105, "Command packet is not aligned"},
281 1.9.2.2 tron {0x0106, "Invalid request ID"},
282 1.9.2.2 tron {0x0107, "Duplicate request ID"},
283 1.9.2.2 tron {0x0108, "ID not locked"},
284 1.9.2.2 tron {0x0109, "LBA out of range"},
285 1.9.2.2 tron {0x010A, "Logical unit not supported"},
286 1.9.2.2 tron {0x010B, "Parameter table does not exist"},
287 1.9.2.2 tron {0x010C, "Parameter index does not exist"},
288 1.9.2.2 tron {0x010D, "Invalid field in CDB"},
289 1.9.2.2 tron {0x010E, "Specified port has invalid drive"},
290 1.9.2.2 tron {0x010F, "Parameter item size mismatch"},
291 1.9.2.2 tron {0x0110, "Failed memory allocation"},
292 1.9.2.2 tron {0x0111, "Memory request too large"},
293 1.9.2.2 tron {0x0112, "Out of memory segments"},
294 1.9.2.2 tron {0x0113, "Invalid address to deallocate"},
295 1.9.2.2 tron {0x0114, "Out of memory"},
296 1.9.2.2 tron {0x0115, "Out of heap"},
297 1.9.2.2 tron {0x0120, "Double degrade"},
298 1.9.2.2 tron {0x0121, "Drive not degraded"},
299 1.9.2.2 tron {0x0122, "Reconstruct error"},
300 1.9.2.2 tron {0x0123, "Replace not accepted"},
301 1.9.2.2 tron {0x0124, "Replace drive capacity too small"},
302 1.9.2.2 tron {0x0125, "Sector count not allowed"},
303 1.9.2.2 tron {0x0126, "No spares left"},
304 1.9.2.2 tron {0x0127, "Reconstruct error"},
305 1.9.2.2 tron {0x0128, "Unit is offline"},
306 1.9.2.2 tron {0x0129, "Cannot update status to DCB"},
307 1.9.2.2 tron {0x0130, "Invalid stripe handle"},
308 1.9.2.2 tron {0x0131, "Handle that was not locked"},
309 1.9.2.2 tron {0x0132, "Handle that was not empy"},
310 1.9.2.2 tron {0x0133, "Handle has different owner"},
311 1.9.2.2 tron {0x0140, "IPR has parent"},
312 1.9.2.2 tron {0x0150, "Illegal Pbuf address alignment"},
313 1.9.2.2 tron {0x0151, "Illegal Pbuf transfer length"},
314 1.9.2.2 tron {0x0152, "Illegal Sbuf address alignment"},
315 1.9.2.2 tron {0x0153, "Illegal Sbuf transfer length"},
316 1.9.2.2 tron {0x0160, "Command packet too large"},
317 1.9.2.2 tron {0x0161, "SGL exceeds maximum length"},
318 1.9.2.2 tron {0x0162, "SGL has too many entries"},
319 1.9.2.2 tron {0x0170, "Insufficient resources for rebuilder"},
320 1.9.2.2 tron {0x0171, "Verify error (data != parity)"},
321 1.9.2.2 tron {0x0180, "Requested segment not in directory of this DCB"},
322 1.9.2.2 tron {0x0181, "DCB segment has unsupported version"},
323 1.9.2.2 tron {0x0182, "DCB segment has checksum error"},
324 1.9.2.2 tron {0x0183, "DCB support (settings) segment invalid"},
325 1.9.2.2 tron {0x0184, "DCB UDB (unit descriptor block) segment invalid"},
326 1.9.2.2 tron {0x0185, "DCB GUID (globally unique identifier) segment invalid"},
327 1.9.2.2 tron {0x01A0, "Could not clear Sbuf"},
328 1.9.2.2 tron {0x01C0, "Flash identify failed"},
329 1.9.2.2 tron {0x01C1, "Flash out of bounds"},
330 1.9.2.2 tron {0x01C2, "Flash verify error"},
331 1.9.2.2 tron {0x01C3, "Flash file object not found"},
332 1.9.2.2 tron {0x01C4, "Flash file already present"},
333 1.9.2.2 tron {0x01C5, "Flash file system full"},
334 1.9.2.2 tron {0x01C6, "Flash file not present"},
335 1.9.2.2 tron {0x01C7, "Flash file size error"},
336 1.9.2.2 tron {0x01C8, "Bad flash file checksum"},
337 1.9.2.2 tron {0x01CA, "Corrupt flash file system detected"},
338 1.9.2.2 tron {0x01D0, "Invalid field in parameter list"},
339 1.9.2.2 tron {0x01D1, "Parameter list length error"},
340 1.9.2.2 tron {0x01D2, "Parameter item is not changeable"},
341 1.9.2.2 tron {0x01D3, "Parameter item is not saveable"},
342 1.9.2.2 tron {0x0200, "UDMA CRC error"},
343 1.9.2.2 tron {0x0201, "Internal CRC error"},
344 1.9.2.2 tron {0x0202, "Data ECC error"},
345 1.9.2.2 tron {0x0203, "ADP level 1 error"},
346 1.9.2.2 tron {0x0204, "Port timeout"},
347 1.9.2.2 tron {0x0205, "Drive power on reset"},
348 1.9.2.2 tron {0x0206, "ADP level 2 error"},
349 1.9.2.2 tron {0x0207, "Soft reset failed"},
350 1.9.2.2 tron {0x0208, "Drive not ready"},
351 1.9.2.2 tron {0x0209, "Unclassified port error"},
352 1.9.2.2 tron {0x020A, "Drive aborted command"},
353 1.9.2.2 tron {0x0210, "Internal CRC error"},
354 1.9.2.2 tron {0x0211, "Host PCI bus abort"},
355 1.9.2.2 tron {0x0212, "Host PCI parity error"},
356 1.9.2.2 tron {0x0213, "Port handler error"},
357 1.9.2.2 tron {0x0214, "Token interrupt count error"},
358 1.9.2.2 tron {0x0215, "Timeout waiting for PCI transfer"},
359 1.9.2.2 tron {0x0216, "Corrected buffer ECC"},
360 1.9.2.2 tron {0x0217, "Uncorrected buffer ECC"},
361 1.9.2.2 tron {0x0230, "Unsupported command during flash recovery"},
362 1.9.2.2 tron {0x0231, "Next image buffer expected"},
363 1.9.2.2 tron {0x0232, "Binary image architecture incompatible"},
364 1.9.2.2 tron {0x0233, "Binary image has no signature"},
365 1.9.2.2 tron {0x0234, "Binary image has bad checksum"},
366 1.9.2.2 tron {0x0235, "Image downloaded overflowed buffer"},
367 1.9.2.2 tron {0x0240, "I2C device not found"},
368 1.9.2.2 tron {0x0241, "I2C transaction aborted"},
369 1.9.2.2 tron {0x0242, "SO-DIMM parameter(s) incompatible using defaults"},
370 1.9.2.2 tron {0x0243, "SO-DIMM unsupported"},
371 1.9.2.2 tron {0x0248, "SPI transfer status error"},
372 1.9.2.2 tron {0x0249, "SPI transfer timeout error"},
373 1.9.2.2 tron {0x0250, "Invalid unit descriptor size in CreateUnit"},
374 1.9.2.2 tron {0x0251, "Unit descriptor size exceeds data buffer in CreateUnit"},
375 1.9.2.2 tron {0x0252, "Invalid value in CreateUnit descriptor"},
376 1.9.2.2 tron {0x0253, "Inadequate disk space to support descriptor in CreateUnit"},
377 1.9.2.2 tron {0x0254, "Unable to create data channel for this unit descriptor"},
378 1.9.2.2 tron {0x0255, "CreateUnit descriptor specifies a drive already in use"},
379 1.9.2.2 tron {0x0256, "Unable to write configuration to all disks during CreateUnit"},
380 1.9.2.2 tron {0x0257, "CreateUnit does not support this descriptor version"},
381 1.9.2.2 tron {0x0258, "Invalid subunit for RAID 0 or 5 in CreateUnit"},
382 1.9.2.2 tron {0x0259, "Too many descriptors in CreateUnit"},
383 1.9.2.2 tron {0x025A, "Invalid configuration specified in CreateUnit descriptor"},
384 1.9.2.2 tron {0x025B, "Invalid LBA offset specified in CreateUnit descriptor"},
385 1.9.2.2 tron {0x025C, "Invalid stripelet size specified in CreateUnit descriptor"},
386 1.9.2.2 tron {0x0260, "SMART attribute exceeded threshold"},
387 1.9.2.2 tron {0xFFFFFFFF, (char *)NULL}
388 1.9.2.2 tron };
389 1.9.2.2 tron
390 1.9.2.2 tron struct twa_pci_identity {
391 1.9.2.2 tron uint32_t vendor_id;
392 1.9.2.2 tron uint32_t product_id;
393 1.9.2.2 tron const char *name;
394 1.9.2.2 tron };
395 1.9.2.2 tron
396 1.9.2.2 tron static const struct twa_pci_identity pci_twa_products[] = {
397 1.9.2.2 tron { PCI_VENDOR_3WARE,
398 1.9.2.2 tron PCI_PRODUCT_3WARE_9000,
399 1.9.2.2 tron "3ware 9000 series",
400 1.9.2.2 tron },
401 1.9.2.2 tron { PCI_VENDOR_3WARE,
402 1.9.2.2 tron PCI_PRODUCT_3WARE_9550,
403 1.9.2.2 tron "3ware 9550SX series",
404 1.9.2.2 tron },
405 1.9.2.2 tron { 0,
406 1.9.2.2 tron 0,
407 1.9.2.2 tron NULL,
408 1.9.2.2 tron },
409 1.9.2.2 tron };
410 1.9.2.2 tron
411 1.9.2.2 tron
412 1.9.2.2 tron static inline void
413 1.9.2.2 tron twa_outl(struct twa_softc *sc, int off, uint32_t val)
414 1.9.2.2 tron {
415 1.9.2.2 tron
416 1.9.2.2 tron bus_space_write_4(sc->twa_bus_iot, sc->twa_bus_ioh, off, val);
417 1.9.2.2 tron bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
418 1.9.2.2 tron BUS_SPACE_BARRIER_WRITE);
419 1.9.2.2 tron }
420 1.9.2.2 tron
421 1.9.2.2 tron static inline uint32_t twa_inl(struct twa_softc *sc, int off)
422 1.9.2.2 tron {
423 1.9.2.2 tron
424 1.9.2.2 tron bus_space_barrier(sc->twa_bus_iot, sc->twa_bus_ioh, off, 4,
425 1.9.2.2 tron BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
426 1.9.2.2 tron return (bus_space_read_4(sc->twa_bus_iot, sc->twa_bus_ioh, off));
427 1.9.2.2 tron }
428 1.9.2.2 tron
429 1.9.2.2 tron void
430 1.9.2.2 tron twa_request_wait_handler(struct twa_request *tr)
431 1.9.2.2 tron {
432 1.9.2.2 tron
433 1.9.2.2 tron wakeup(tr);
434 1.9.2.2 tron }
435 1.9.2.2 tron
436 1.9.2.2 tron static int
437 1.9.2.2 tron twa_match(struct device *parent, struct cfdata *cfdata, void *aux)
438 1.9.2.2 tron {
439 1.9.2.2 tron int i;
440 1.9.2.2 tron struct pci_attach_args *pa = aux;
441 1.9.2.2 tron const struct twa_pci_identity *entry = 0;
442 1.9.2.2 tron
443 1.9.2.2 tron if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE) {
444 1.9.2.2 tron for (i = 0; (pci_twa_products[i].product_id); i++) {
445 1.9.2.2 tron entry = &pci_twa_products[i];
446 1.9.2.2 tron if (entry->product_id == PCI_PRODUCT(pa->pa_id)) {
447 1.9.2.2 tron aprint_normal("%s: (rev. 0x%02x)\n",
448 1.9.2.2 tron entry->name, PCI_REVISION(pa->pa_class));
449 1.9.2.2 tron return (1);
450 1.9.2.2 tron }
451 1.9.2.2 tron }
452 1.9.2.2 tron }
453 1.9.2.2 tron return (0);
454 1.9.2.2 tron }
455 1.9.2.2 tron
456 1.9.2.2 tron static const char *
457 1.9.2.2 tron twa_find_msg_string(const struct twa_message *table, uint16_t code)
458 1.9.2.2 tron {
459 1.9.2.2 tron int i;
460 1.9.2.2 tron
461 1.9.2.2 tron for (i = 0; table[i].message != NULL; i++)
462 1.9.2.2 tron if (table[i].code == code)
463 1.9.2.2 tron return(table[i].message);
464 1.9.2.2 tron
465 1.9.2.2 tron return(table[i].message);
466 1.9.2.2 tron }
467 1.9.2.2 tron
468 1.9.2.2 tron void
469 1.9.2.2 tron twa_release_request(struct twa_request *tr)
470 1.9.2.2 tron {
471 1.9.2.2 tron int s;
472 1.9.2.2 tron struct twa_softc *sc;
473 1.9.2.2 tron
474 1.9.2.2 tron sc = tr->tr_sc;
475 1.9.2.2 tron
476 1.9.2.2 tron if ((tr->tr_flags & TWA_CMD_AEN) == 0) {
477 1.9.2.2 tron s = splbio();
478 1.9.2.2 tron TAILQ_INSERT_TAIL(&tr->tr_sc->twa_free, tr, tr_link);
479 1.9.2.2 tron splx(s);
480 1.9.2.2 tron if (__predict_false((tr->tr_sc->twa_sc_flags &
481 1.9.2.2 tron TWA_STATE_REQUEST_WAIT) != 0)) {
482 1.9.2.2 tron tr->tr_sc->twa_sc_flags &= ~TWA_STATE_REQUEST_WAIT;
483 1.9.2.2 tron wakeup(&sc->twa_free);
484 1.9.2.2 tron }
485 1.9.2.2 tron } else
486 1.9.2.2 tron tr->tr_flags &= ~TWA_CMD_AEN_BUSY;
487 1.9.2.2 tron }
488 1.9.2.2 tron
489 1.9.2.2 tron static void
490 1.9.2.2 tron twa_unmap_request(struct twa_request *tr)
491 1.9.2.2 tron {
492 1.9.2.2 tron struct twa_softc *sc = tr->tr_sc;
493 1.9.2.2 tron uint8_t cmd_status;
494 1.9.2.2 tron int s;
495 1.9.2.2 tron
496 1.9.2.2 tron /* If the command involved data, unmap that too. */
497 1.9.2.2 tron if (tr->tr_data != NULL) {
498 1.9.2.2 tron if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K)
499 1.9.2.2 tron cmd_status = tr->tr_command->command.cmd_pkt_9k.status;
500 1.9.2.2 tron else
501 1.9.2.2 tron cmd_status =
502 1.9.2.2 tron tr->tr_command->command.cmd_pkt_7k.generic.status;
503 1.9.2.2 tron
504 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_OUT) {
505 1.9.2.2 tron bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
506 1.9.2.2 tron 0, tr->tr_length, BUS_DMASYNC_POSTREAD);
507 1.9.2.2 tron /*
508 1.9.2.2 tron * If we are using a bounce buffer, and we are reading
509 1.9.2.2 tron * data, copy the real data in.
510 1.9.2.2 tron */
511 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
512 1.9.2.2 tron if (cmd_status == 0)
513 1.9.2.2 tron memcpy(tr->tr_real_data, tr->tr_data,
514 1.9.2.2 tron tr->tr_real_length);
515 1.9.2.2 tron }
516 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_IN)
517 1.9.2.2 tron bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map,
518 1.9.2.2 tron 0, tr->tr_length, BUS_DMASYNC_POSTWRITE);
519 1.9.2.2 tron
520 1.9.2.2 tron bus_dmamap_unload(sc->twa_dma_tag, tr->tr_dma_map);
521 1.9.2.2 tron }
522 1.9.2.2 tron
523 1.9.2.2 tron /* Free alignment buffer if it was used. */
524 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
525 1.9.2.2 tron s = splvm();
526 1.9.2.2 tron uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
527 1.9.2.2 tron tr->tr_length);
528 1.9.2.2 tron splx(s);
529 1.9.2.2 tron tr->tr_data = tr->tr_real_data;
530 1.9.2.2 tron tr->tr_length = tr->tr_real_length;
531 1.9.2.2 tron }
532 1.9.2.2 tron }
533 1.9.2.2 tron
534 1.9.2.2 tron /*
535 1.9.2.2 tron * Function name: twa_wait_request
536 1.9.2.2 tron * Description: Sends down a firmware cmd, and waits for the completion,
537 1.9.2.2 tron * but NOT in a tight loop.
538 1.9.2.2 tron *
539 1.9.2.2 tron * Input: tr -- ptr to request pkt
540 1.9.2.2 tron * timeout -- max # of seconds to wait before giving up
541 1.9.2.2 tron * Output: None
542 1.9.2.2 tron * Return value: 0 -- success
543 1.9.2.2 tron * non-zero-- failure
544 1.9.2.2 tron */
545 1.9.2.2 tron static int
546 1.9.2.2 tron twa_wait_request(struct twa_request *tr, uint32_t timeout)
547 1.9.2.2 tron {
548 1.9.2.2 tron time_t end_time;
549 1.9.2.2 tron struct timeval t1;
550 1.9.2.2 tron int s, rv;
551 1.9.2.2 tron
552 1.9.2.2 tron tr->tr_flags |= TWA_CMD_SLEEP_ON_REQUEST;
553 1.9.2.2 tron tr->tr_callback = twa_request_wait_handler;
554 1.9.2.2 tron tr->tr_status = TWA_CMD_BUSY;
555 1.9.2.2 tron
556 1.9.2.2 tron rv = twa_map_request(tr);
557 1.9.2.2 tron
558 1.9.2.2 tron if (rv != 0)
559 1.9.2.2 tron return (rv);
560 1.9.2.2 tron
561 1.9.2.2 tron microtime(&t1);
562 1.9.2.2 tron end_time = t1.tv_usec +
563 1.9.2.2 tron (timeout * 1000 * 100);
564 1.9.2.2 tron
565 1.9.2.2 tron while (tr->tr_status != TWA_CMD_COMPLETE) {
566 1.9.2.2 tron rv = tr->tr_error;
567 1.9.2.2 tron if (rv != 0)
568 1.9.2.2 tron return(rv);
569 1.9.2.2 tron if ((rv = tsleep(tr, PRIBIO, "twawait", timeout * hz)) == 0)
570 1.9.2.2 tron break;
571 1.9.2.2 tron
572 1.9.2.2 tron if (rv == EWOULDBLOCK) {
573 1.9.2.2 tron /*
574 1.9.2.2 tron * We will reset the controller only if the request has
575 1.9.2.2 tron * already been submitted, so as to not lose the
576 1.9.2.2 tron * request packet. If a busy request timed out, the
577 1.9.2.2 tron * reset will take care of freeing resources. If a
578 1.9.2.2 tron * pending request timed out, we will free resources
579 1.9.2.2 tron * for that request, right here. So, the caller is
580 1.9.2.2 tron * expected to NOT cleanup when ETIMEDOUT is returned.
581 1.9.2.2 tron */
582 1.9.2.2 tron if (tr->tr_status == TWA_CMD_BUSY)
583 1.9.2.2 tron twa_reset(tr->tr_sc);
584 1.9.2.2 tron else {
585 1.9.2.2 tron /* Request was never submitted. Clean up. */
586 1.9.2.2 tron s = splbio();
587 1.9.2.2 tron TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr,
588 1.9.2.2 tron tr_link);
589 1.9.2.2 tron splx(s);
590 1.9.2.2 tron
591 1.9.2.2 tron twa_unmap_request(tr);
592 1.9.2.2 tron if (tr->tr_data)
593 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
594 1.9.2.2 tron
595 1.9.2.2 tron twa_release_request(tr);
596 1.9.2.2 tron }
597 1.9.2.2 tron return(ETIMEDOUT);
598 1.9.2.2 tron }
599 1.9.2.2 tron /*
600 1.9.2.2 tron * Either the request got completed, or we were woken up by a
601 1.9.2.2 tron * signal. Calculate the new timeout, in case it was the
602 1.9.2.2 tron * latter.
603 1.9.2.2 tron */
604 1.9.2.2 tron microtime(&t1);
605 1.9.2.2 tron
606 1.9.2.2 tron timeout = (end_time - t1.tv_usec) / (1000 * 100);
607 1.9.2.2 tron }
608 1.9.2.2 tron return(rv);
609 1.9.2.2 tron }
610 1.9.2.2 tron
611 1.9.2.2 tron /*
612 1.9.2.2 tron * Function name: twa_immediate_request
613 1.9.2.2 tron * Description: Sends down a firmware cmd, and waits for the completion
614 1.9.2.2 tron * in a tight loop.
615 1.9.2.2 tron *
616 1.9.2.2 tron * Input: tr -- ptr to request pkt
617 1.9.2.2 tron * timeout -- max # of seconds to wait before giving up
618 1.9.2.2 tron * Output: None
619 1.9.2.2 tron * Return value: 0 -- success
620 1.9.2.2 tron * non-zero-- failure
621 1.9.2.2 tron */
622 1.9.2.2 tron static int
623 1.9.2.2 tron twa_immediate_request(struct twa_request *tr, uint32_t timeout)
624 1.9.2.2 tron {
625 1.9.2.2 tron struct timeval t1;
626 1.9.2.2 tron int s = 0, rv = 0;
627 1.9.2.2 tron
628 1.9.2.2 tron rv = twa_map_request(tr);
629 1.9.2.2 tron
630 1.9.2.2 tron if (rv != 0)
631 1.9.2.2 tron return(rv);
632 1.9.2.2 tron
633 1.9.2.2 tron timeout = (timeout * 10000 * 10);
634 1.9.2.2 tron
635 1.9.2.2 tron microtime(&t1);
636 1.9.2.2 tron
637 1.9.2.2 tron timeout += t1.tv_usec;
638 1.9.2.2 tron
639 1.9.2.2 tron do {
640 1.9.2.2 tron rv = tr->tr_error;
641 1.9.2.2 tron if (rv != 0)
642 1.9.2.2 tron return(rv);
643 1.9.2.2 tron s = splbio();
644 1.9.2.2 tron twa_done(tr->tr_sc);
645 1.9.2.2 tron splx(s);
646 1.9.2.2 tron if (tr->tr_status == TWA_CMD_COMPLETE)
647 1.9.2.2 tron return(rv);
648 1.9.2.2 tron microtime(&t1);
649 1.9.2.2 tron } while (t1.tv_usec <= timeout);
650 1.9.2.2 tron
651 1.9.2.2 tron /*
652 1.9.2.2 tron * We will reset the controller only if the request has
653 1.9.2.2 tron * already been submitted, so as to not lose the
654 1.9.2.2 tron * request packet. If a busy request timed out, the
655 1.9.2.2 tron * reset will take care of freeing resources. If a
656 1.9.2.2 tron * pending request timed out, we will free resources
657 1.9.2.2 tron * for that request, right here. So, the caller is
658 1.9.2.2 tron * expected to NOT cleanup when ETIMEDOUT is returned.
659 1.9.2.2 tron */
660 1.9.2.2 tron rv = ETIMEDOUT;
661 1.9.2.2 tron
662 1.9.2.2 tron if (tr->tr_status == TWA_CMD_BUSY)
663 1.9.2.2 tron twa_reset(tr->tr_sc);
664 1.9.2.2 tron else {
665 1.9.2.2 tron /* Request was never submitted. Clean up. */
666 1.9.2.2 tron s = splbio();
667 1.9.2.2 tron TAILQ_REMOVE(&tr->tr_sc->twa_pending, tr, tr_link);
668 1.9.2.2 tron splx(s);
669 1.9.2.2 tron twa_unmap_request(tr);
670 1.9.2.2 tron if (tr->tr_data)
671 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
672 1.9.2.2 tron
673 1.9.2.2 tron twa_release_request(tr);
674 1.9.2.2 tron }
675 1.9.2.2 tron return (rv);
676 1.9.2.2 tron }
677 1.9.2.2 tron
678 1.9.2.2 tron static int
679 1.9.2.2 tron twa_inquiry(struct twa_request *tr, int lunid)
680 1.9.2.2 tron {
681 1.9.2.2 tron int error;
682 1.9.2.2 tron struct twa_command_9k *tr_9k_cmd;
683 1.9.2.2 tron
684 1.9.2.2 tron if (tr->tr_data == NULL)
685 1.9.2.2 tron return (ENOMEM);
686 1.9.2.2 tron
687 1.9.2.2 tron memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
688 1.9.2.2 tron
689 1.9.2.2 tron tr->tr_length = TWA_SECTOR_SIZE;
690 1.9.2.2 tron tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
691 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_IN;
692 1.9.2.2 tron
693 1.9.2.2 tron tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
694 1.9.2.2 tron
695 1.9.2.2 tron tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
696 1.9.2.2 tron tr_9k_cmd->unit = lunid;
697 1.9.2.2 tron tr_9k_cmd->request_id = tr->tr_request_id;
698 1.9.2.2 tron tr_9k_cmd->status = 0;
699 1.9.2.2 tron tr_9k_cmd->sgl_offset = 16;
700 1.9.2.2 tron tr_9k_cmd->sgl_entries = 1;
701 1.9.2.2 tron /* create the CDB here */
702 1.9.2.2 tron tr_9k_cmd->cdb[0] = INQUIRY;
703 1.9.2.2 tron tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
704 1.9.2.2 tron tr_9k_cmd->cdb[4] = 255;
705 1.9.2.2 tron
706 1.9.2.2 tron /* XXXX setup page data no lun device
707 1.9.2.2 tron * it seems 9000 series does not indicate
708 1.9.2.2 tron * NOTPRESENT - need more investigation
709 1.9.2.2 tron */
710 1.9.2.2 tron ((struct scsipi_inquiry_data *)tr->tr_data)->device =
711 1.9.2.2 tron SID_QUAL_LU_NOTPRESENT;
712 1.9.2.2 tron
713 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
714 1.9.2.2 tron
715 1.9.2.2 tron if (error != 0)
716 1.9.2.2 tron return (error);
717 1.9.2.2 tron
718 1.9.2.2 tron if (((struct scsipi_inquiry_data *)tr->tr_data)->device ==
719 1.9.2.2 tron SID_QUAL_LU_NOTPRESENT)
720 1.9.2.2 tron error = 1;
721 1.9.2.2 tron
722 1.9.2.2 tron return (error);
723 1.9.2.2 tron }
724 1.9.2.2 tron
725 1.9.2.2 tron static int
726 1.9.2.2 tron twa_print_inquiry_data(struct twa_softc *sc, struct scsipi_inquiry_data *scsipi)
727 1.9.2.2 tron {
728 1.9.2.2 tron
729 1.9.2.2 tron printf("%s: %s\n", sc->twa_dv.dv_xname, scsipi->vendor);
730 1.9.2.2 tron
731 1.9.2.2 tron return (1);
732 1.9.2.2 tron }
733 1.9.2.2 tron
734 1.9.2.2 tron
735 1.9.2.2 tron static uint64_t
736 1.9.2.2 tron twa_read_capacity(struct twa_request *tr, int lunid)
737 1.9.2.2 tron {
738 1.9.2.2 tron int error;
739 1.9.2.2 tron struct twa_command_9k *tr_9k_cmd;
740 1.9.2.2 tron uint64_t array_size = 0LL;
741 1.9.2.2 tron
742 1.9.2.2 tron if (tr->tr_data == NULL)
743 1.9.2.2 tron return (ENOMEM);
744 1.9.2.2 tron
745 1.9.2.2 tron memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
746 1.9.2.2 tron
747 1.9.2.2 tron tr->tr_length = TWA_SECTOR_SIZE;
748 1.9.2.2 tron tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
749 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_OUT;
750 1.9.2.2 tron
751 1.9.2.2 tron tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
752 1.9.2.2 tron
753 1.9.2.2 tron tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
754 1.9.2.2 tron tr_9k_cmd->unit = lunid;
755 1.9.2.2 tron tr_9k_cmd->request_id = tr->tr_request_id;
756 1.9.2.2 tron tr_9k_cmd->status = 0;
757 1.9.2.2 tron tr_9k_cmd->sgl_offset = 16;
758 1.9.2.2 tron tr_9k_cmd->sgl_entries = 1;
759 1.9.2.2 tron /* create the CDB here */
760 1.9.2.2 tron tr_9k_cmd->cdb[0] = READ_CAPACITY_16;
761 1.9.2.2 tron tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e) | SRC16_SERVICE_ACTION;
762 1.9.2.2 tron
763 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
764 1.9.2.2 tron
765 1.9.2.2 tron if (error == 0) {
766 1.9.2.2 tron #if BYTE_ORDER == BIG_ENDIAN
767 1.9.2.2 tron array_size = bswap64(_8btol(
768 1.9.2.2 tron ((struct scsipi_read_capacity_16_data *)tr->tr_data->addr) + 1);
769 1.9.2.2 tron #else
770 1.9.2.2 tron array_size = _8btol(((struct scsipi_read_capacity_16_data *)
771 1.9.2.2 tron tr->tr_data)->addr) + 1;
772 1.9.2.2 tron #endif
773 1.9.2.2 tron }
774 1.9.2.2 tron return (array_size);
775 1.9.2.2 tron }
776 1.9.2.2 tron
777 1.9.2.2 tron static int
778 1.9.2.2 tron twa_request_sense(struct twa_request *tr, int lunid)
779 1.9.2.2 tron {
780 1.9.2.2 tron int error = 1;
781 1.9.2.2 tron struct twa_command_9k *tr_9k_cmd;
782 1.9.2.2 tron
783 1.9.2.2 tron if (tr->tr_data == NULL)
784 1.9.2.2 tron return (error);
785 1.9.2.2 tron
786 1.9.2.2 tron memset(tr->tr_data, 0, TWA_SECTOR_SIZE);
787 1.9.2.2 tron
788 1.9.2.2 tron tr->tr_length = TWA_SECTOR_SIZE;
789 1.9.2.2 tron tr->tr_cmd_pkt_type = TWA_CMD_PKT_TYPE_9K;
790 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_OUT;
791 1.9.2.2 tron
792 1.9.2.2 tron tr_9k_cmd = &tr->tr_command->command.cmd_pkt_9k;
793 1.9.2.2 tron
794 1.9.2.2 tron tr_9k_cmd->command.opcode = TWA_OP_EXECUTE_SCSI_COMMAND;
795 1.9.2.2 tron tr_9k_cmd->unit = lunid;
796 1.9.2.2 tron tr_9k_cmd->request_id = tr->tr_request_id;
797 1.9.2.2 tron tr_9k_cmd->status = 0;
798 1.9.2.2 tron tr_9k_cmd->sgl_offset = 16;
799 1.9.2.2 tron tr_9k_cmd->sgl_entries = 1;
800 1.9.2.2 tron /* create the CDB here */
801 1.9.2.2 tron tr_9k_cmd->cdb[0] = SCSI_REQUEST_SENSE;
802 1.9.2.2 tron tr_9k_cmd->cdb[1] = ((lunid << 5) & 0x0e);
803 1.9.2.2 tron tr_9k_cmd->cdb[4] = 255;
804 1.9.2.2 tron
805 1.9.2.2 tron /*XXX AEN notification called in interrupt context
806 1.9.2.2 tron * so just queue the request. Return as quickly
807 1.9.2.2 tron * as possible from interrupt
808 1.9.2.2 tron */
809 1.9.2.2 tron if ((tr->tr_flags & TWA_CMD_AEN) != 0)
810 1.9.2.2 tron error = twa_map_request(tr);
811 1.9.2.2 tron else
812 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
813 1.9.2.2 tron
814 1.9.2.2 tron return (error);
815 1.9.2.2 tron }
816 1.9.2.2 tron
817 1.9.2.2 tron static int
818 1.9.2.2 tron twa_alloc_req_pkts(struct twa_softc *sc, int num_reqs)
819 1.9.2.2 tron {
820 1.9.2.2 tron struct twa_request *tr;
821 1.9.2.2 tron struct twa_command_packet *tc;
822 1.9.2.2 tron bus_dma_segment_t seg;
823 1.9.2.2 tron size_t max_segs, max_xfer;
824 1.9.2.2 tron int i, rv, rseg, size;
825 1.9.2.2 tron
826 1.9.2.2 tron if ((sc->twa_req_buf = malloc(num_reqs * sizeof(struct twa_request),
827 1.9.2.2 tron M_DEVBUF, M_NOWAIT)) == NULL)
828 1.9.2.2 tron return(ENOMEM);
829 1.9.2.2 tron
830 1.9.2.2 tron size = num_reqs * sizeof(struct twa_command_packet);
831 1.9.2.2 tron
832 1.9.2.2 tron /* Allocate memory for cmd pkts. */
833 1.9.2.2 tron if ((rv = bus_dmamem_alloc(sc->twa_dma_tag,
834 1.9.2.2 tron size, PAGE_SIZE, 0, &seg,
835 1.9.2.2 tron 1, &rseg, BUS_DMA_NOWAIT)) != 0){
836 1.9.2.2 tron aprint_error("%s: unable to allocate "
837 1.9.2.2 tron "command packets, rv = %d\n",
838 1.9.2.2 tron sc->twa_dv.dv_xname, rv);
839 1.9.2.2 tron return (ENOMEM);
840 1.9.2.2 tron }
841 1.9.2.2 tron
842 1.9.2.2 tron if ((rv = bus_dmamem_map(sc->twa_dma_tag,
843 1.9.2.2 tron &seg, rseg, size, (caddr_t *)&sc->twa_cmds,
844 1.9.2.2 tron BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
845 1.9.2.2 tron aprint_error("%s: unable to map commands, rv = %d\n",
846 1.9.2.2 tron sc->twa_dv.dv_xname, rv);
847 1.9.2.2 tron return (1);
848 1.9.2.2 tron }
849 1.9.2.2 tron
850 1.9.2.2 tron if ((rv = bus_dmamap_create(sc->twa_dma_tag,
851 1.9.2.2 tron size, num_reqs, size,
852 1.9.2.2 tron 0, BUS_DMA_NOWAIT, &sc->twa_cmd_map)) != 0) {
853 1.9.2.2 tron aprint_error("%s: unable to create command DMA map, "
854 1.9.2.2 tron "rv = %d\n", sc->twa_dv.dv_xname, rv);
855 1.9.2.2 tron return (ENOMEM);
856 1.9.2.2 tron }
857 1.9.2.2 tron
858 1.9.2.2 tron if ((rv = bus_dmamap_load(sc->twa_dma_tag, sc->twa_cmd_map,
859 1.9.2.2 tron sc->twa_cmds, size, NULL,
860 1.9.2.2 tron BUS_DMA_NOWAIT)) != 0) {
861 1.9.2.2 tron aprint_error("%s: unable to load command DMA map, "
862 1.9.2.2 tron "rv = %d\n", sc->twa_dv.dv_xname, rv);
863 1.9.2.2 tron return (1);
864 1.9.2.2 tron }
865 1.9.2.2 tron
866 1.9.2.2 tron if ((uintptr_t)sc->twa_cmds % TWA_ALIGNMENT) {
867 1.9.2.2 tron aprint_error("%s: DMA map memory not aligned on %d boundary\n",
868 1.9.2.2 tron sc->twa_dv.dv_xname, TWA_ALIGNMENT);
869 1.9.2.2 tron
870 1.9.2.2 tron return (1);
871 1.9.2.2 tron }
872 1.9.2.2 tron tc = sc->twa_cmd_pkt_buf = (struct twa_command_packet *)sc->twa_cmds;
873 1.9.2.2 tron sc->twa_cmd_pkt_phys = sc->twa_cmd_map->dm_segs[0].ds_addr;
874 1.9.2.2 tron
875 1.9.2.2 tron memset(sc->twa_req_buf, 0, num_reqs * sizeof(struct twa_request));
876 1.9.2.2 tron memset(sc->twa_cmd_pkt_buf, 0,
877 1.9.2.2 tron num_reqs * sizeof(struct twa_command_packet));
878 1.9.2.2 tron
879 1.9.2.2 tron sc->sc_twa_request = sc->twa_req_buf;
880 1.9.2.2 tron max_segs = twa_get_maxsegs();
881 1.9.2.2 tron max_xfer = twa_get_maxxfer(max_segs);
882 1.9.2.2 tron
883 1.9.2.2 tron for (i = 0; i < num_reqs; i++, tc++) {
884 1.9.2.2 tron tr = &(sc->twa_req_buf[i]);
885 1.9.2.2 tron tr->tr_command = tc;
886 1.9.2.2 tron tr->tr_cmd_phys = sc->twa_cmd_pkt_phys +
887 1.9.2.2 tron (i * sizeof(struct twa_command_packet));
888 1.9.2.2 tron tr->tr_request_id = i;
889 1.9.2.2 tron tr->tr_sc = sc;
890 1.9.2.2 tron
891 1.9.2.2 tron /*
892 1.9.2.2 tron * Create a map for data buffers. maxsize (256 * 1024) used in
893 1.9.2.2 tron * bus_dma_tag_create above should suffice the bounce page needs
894 1.9.2.2 tron * for data buffers, since the max I/O size we support is 128KB.
895 1.9.2.2 tron * If we supported I/O's bigger than 256KB, we would have to
896 1.9.2.2 tron * create a second dma_tag, with the appropriate maxsize.
897 1.9.2.2 tron */
898 1.9.2.2 tron if ((rv = bus_dmamap_create(sc->twa_dma_tag,
899 1.9.2.2 tron max_xfer, max_segs, 1, 0, BUS_DMA_NOWAIT,
900 1.9.2.2 tron &tr->tr_dma_map)) != 0) {
901 1.9.2.2 tron aprint_error("%s: unable to create command "
902 1.9.2.2 tron "DMA map, rv = %d\n",
903 1.9.2.2 tron sc->twa_dv.dv_xname, rv);
904 1.9.2.2 tron return (ENOMEM);
905 1.9.2.2 tron }
906 1.9.2.2 tron /* Insert request into the free queue. */
907 1.9.2.2 tron if (i != 0) {
908 1.9.2.2 tron sc->twa_lookup[i] = tr;
909 1.9.2.2 tron twa_release_request(tr);
910 1.9.2.2 tron } else
911 1.9.2.2 tron tr->tr_flags |= TWA_CMD_AEN;
912 1.9.2.2 tron }
913 1.9.2.2 tron return(0);
914 1.9.2.2 tron }
915 1.9.2.2 tron
916 1.9.2.2 tron static void
917 1.9.2.2 tron twa_recompute_openings(struct twa_softc *sc)
918 1.9.2.2 tron {
919 1.9.2.2 tron struct twa_drive *td;
920 1.9.2.2 tron int unit;
921 1.9.2.2 tron int openings;
922 1.9.2.2 tron
923 1.9.2.2 tron if (sc->sc_nunits != 0)
924 1.9.2.2 tron openings = ((TWA_Q_LENGTH / 2) / sc->sc_nunits);
925 1.9.2.2 tron else
926 1.9.2.2 tron openings = 0;
927 1.9.2.2 tron if (openings == sc->sc_openings)
928 1.9.2.2 tron return;
929 1.9.2.2 tron sc->sc_openings = openings;
930 1.9.2.2 tron
931 1.9.2.2 tron #ifdef TWA_DEBUG
932 1.9.2.2 tron printf("%s: %d array%s, %d openings per array\n",
933 1.9.2.2 tron sc->sc_twa.dv_xname, sc->sc_nunits,
934 1.9.2.2 tron sc->sc_nunits == 1 ? "" : "s", sc->sc_openings);
935 1.9.2.2 tron #endif
936 1.9.2.2 tron for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
937 1.9.2.2 tron td = &sc->sc_units[unit];
938 1.9.2.2 tron if (td->td_dev != NULL)
939 1.9.2.2 tron (*td->td_callbacks->tcb_openings)(td->td_dev,
940 1.9.2.2 tron sc->sc_openings);
941 1.9.2.2 tron }
942 1.9.2.2 tron }
943 1.9.2.2 tron
944 1.9.2.2 tron static int
945 1.9.2.2 tron twa_request_bus_scan(struct twa_softc *sc)
946 1.9.2.2 tron {
947 1.9.2.2 tron struct twa_drive *td;
948 1.9.2.2 tron struct twa_request *tr;
949 1.9.2.2 tron struct twa_attach_args twaa;
950 1.9.2.2 tron int help[2];
951 1.9.2.2 tron locdesc_t *ldesc = (void *)help; /* XXX */
952 1.9.2.2 tron int s, unit;
953 1.9.2.2 tron
954 1.9.2.2 tron s = splbio();
955 1.9.2.2 tron for (unit = 0; unit < TWA_MAX_UNITS; unit++) {
956 1.9.2.2 tron
957 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL) {
958 1.9.2.2 tron splx(s);
959 1.9.2.2 tron return (EIO);
960 1.9.2.2 tron }
961 1.9.2.2 tron
962 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
963 1.9.2.2 tron
964 1.9.2.2 tron tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
965 1.9.2.2 tron
966 1.9.2.2 tron if (tr->tr_data == NULL) {
967 1.9.2.2 tron twa_release_request(tr);
968 1.9.2.2 tron splx(s);
969 1.9.2.2 tron return (ENOMEM);
970 1.9.2.2 tron }
971 1.9.2.2 tron td = &sc->sc_units[unit];
972 1.9.2.2 tron
973 1.9.2.2 tron if (twa_inquiry(tr, unit) == 0) {
974 1.9.2.2 tron if (td->td_dev == NULL) {
975 1.9.2.2 tron twa_print_inquiry_data(sc,
976 1.9.2.2 tron ((struct scsipi_inquiry_data *)tr->tr_data));
977 1.9.2.2 tron
978 1.9.2.2 tron sc->sc_nunits++;
979 1.9.2.2 tron
980 1.9.2.2 tron sc->sc_units[unit].td_size =
981 1.9.2.2 tron twa_read_capacity(tr, unit);
982 1.9.2.2 tron
983 1.9.2.2 tron twaa.twaa_unit = unit;
984 1.9.2.2 tron
985 1.9.2.2 tron twa_recompute_openings(sc);
986 1.9.2.2 tron
987 1.9.2.2 tron ldesc->len = 1;
988 1.9.2.2 tron ldesc->locs[TWACF_UNIT] = unit;
989 1.9.2.2 tron
990 1.9.2.2 tron sc->sc_units[unit].td_dev =
991 1.9.2.2 tron config_found_sm_loc(&sc->twa_dv, "twa",
992 1.9.2.2 tron ldesc, &twaa, twa_print, twa_submatch);
993 1.9.2.2 tron }
994 1.9.2.2 tron } else {
995 1.9.2.2 tron if (td->td_dev != NULL) {
996 1.9.2.2 tron sc->sc_nunits--;
997 1.9.2.2 tron
998 1.9.2.2 tron (void) config_detach(td->td_dev, DETACH_FORCE);
999 1.9.2.2 tron td->td_dev = NULL;
1000 1.9.2.2 tron td->td_size = 0;
1001 1.9.2.2 tron
1002 1.9.2.2 tron twa_recompute_openings(sc);
1003 1.9.2.2 tron }
1004 1.9.2.2 tron }
1005 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
1006 1.9.2.2 tron
1007 1.9.2.2 tron twa_release_request(tr);
1008 1.9.2.2 tron }
1009 1.9.2.2 tron splx(s);
1010 1.9.2.2 tron
1011 1.9.2.2 tron return (0);
1012 1.9.2.2 tron }
1013 1.9.2.2 tron
1014 1.9.2.2 tron /*
1015 1.9.2.2 tron * Match a sub-device.
1016 1.9.2.2 tron */
1017 1.9.2.2 tron static int
1018 1.9.2.2 tron twa_submatch(struct device *parent, struct cfdata *cf,
1019 1.9.2.2 tron const locdesc_t *ldesc, void *aux)
1020 1.9.2.2 tron {
1021 1.9.2.2 tron
1022 1.9.2.2 tron if (cf->cf_loc[TWACF_UNIT] != TWACF_UNIT_DEFAULT &&
1023 1.9.2.2 tron cf->cf_loc[TWACF_UNIT] != ldesc->locs[TWACF_UNIT])
1024 1.9.2.2 tron return (0);
1025 1.9.2.2 tron
1026 1.9.2.2 tron return (config_match(parent, cf, aux));
1027 1.9.2.2 tron }
1028 1.9.2.2 tron
1029 1.9.2.2 tron
1030 1.9.2.2 tron
1031 1.9.2.2 tron #ifdef DIAGNOSTIC
1032 1.9.2.2 tron static inline void
1033 1.9.2.2 tron twa_check_busy_q(struct twa_request *tr)
1034 1.9.2.2 tron {
1035 1.9.2.2 tron struct twa_request *rq;
1036 1.9.2.2 tron struct twa_softc *sc = tr->tr_sc;
1037 1.9.2.2 tron
1038 1.9.2.2 tron TAILQ_FOREACH(rq, &sc->twa_busy, tr_link) {
1039 1.9.2.2 tron if (tr->tr_request_id == rq->tr_request_id) {
1040 1.9.2.2 tron panic("cannot submit same request more than once");
1041 1.9.2.2 tron } else if (tr->bp == rq->bp && tr->bp != 0) {
1042 1.9.2.2 tron /* XXX A check for 0 for the buf ptr is needed to
1043 1.9.2.2 tron * guard against ioctl requests with a buf ptr of
1044 1.9.2.2 tron * 0 and also aen notifications. Looking for
1045 1.9.2.2 tron * external cmds only.
1046 1.9.2.2 tron */
1047 1.9.2.2 tron panic("cannot submit same buf more than once");
1048 1.9.2.2 tron } else {
1049 1.9.2.2 tron /* Empty else statement */
1050 1.9.2.2 tron }
1051 1.9.2.2 tron }
1052 1.9.2.2 tron }
1053 1.9.2.2 tron #endif
1054 1.9.2.2 tron
1055 1.9.2.2 tron static int
1056 1.9.2.2 tron twa_start(struct twa_request *tr)
1057 1.9.2.2 tron {
1058 1.9.2.2 tron struct twa_softc *sc = tr->tr_sc;
1059 1.9.2.2 tron uint32_t status_reg;
1060 1.9.2.2 tron int s;
1061 1.9.2.2 tron int error;
1062 1.9.2.2 tron
1063 1.9.2.2 tron s = splbio();
1064 1.9.2.2 tron /* Check to see if we can post a command. */
1065 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1066 1.9.2.2 tron if ((error = twa_check_ctlr_state(sc, status_reg)))
1067 1.9.2.2 tron goto out;
1068 1.9.2.2 tron
1069 1.9.2.2 tron if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
1070 1.9.2.2 tron if (tr->tr_status != TWA_CMD_PENDING) {
1071 1.9.2.2 tron tr->tr_status = TWA_CMD_PENDING;
1072 1.9.2.2 tron TAILQ_INSERT_TAIL(&tr->tr_sc->twa_pending,
1073 1.9.2.2 tron tr, tr_link);
1074 1.9.2.2 tron }
1075 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1076 1.9.2.2 tron TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
1077 1.9.2.2 tron error = EBUSY;
1078 1.9.2.2 tron } else {
1079 1.9.2.2 tron bus_dmamap_sync(sc->twa_dma_tag, sc->twa_cmd_map,
1080 1.9.2.2 tron (caddr_t)tr->tr_command - sc->twa_cmds,
1081 1.9.2.2 tron sizeof(struct twa_command_packet),
1082 1.9.2.2 tron BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1083 1.9.2.2 tron
1084 1.9.2.2 tron /* Cmd queue is not full. Post the command. */
1085 1.9.2.2 tron TWA_WRITE_COMMAND_QUEUE(sc, tr->tr_cmd_phys +
1086 1.9.2.2 tron sizeof(struct twa_command_header));
1087 1.9.2.2 tron
1088 1.9.2.2 tron /* Mark the request as currently being processed. */
1089 1.9.2.2 tron tr->tr_status = TWA_CMD_BUSY;
1090 1.9.2.2 tron
1091 1.9.2.2 tron #ifdef DIAGNOSTIC
1092 1.9.2.2 tron twa_check_busy_q(tr);
1093 1.9.2.2 tron #endif
1094 1.9.2.2 tron
1095 1.9.2.2 tron /* Move the request into the busy queue. */
1096 1.9.2.2 tron TAILQ_INSERT_TAIL(&tr->tr_sc->twa_busy, tr, tr_link);
1097 1.9.2.2 tron }
1098 1.9.2.2 tron out:
1099 1.9.2.2 tron splx(s);
1100 1.9.2.2 tron return(error);
1101 1.9.2.2 tron }
1102 1.9.2.2 tron
1103 1.9.2.2 tron static int
1104 1.9.2.2 tron twa_drain_response_queue(struct twa_softc *sc)
1105 1.9.2.2 tron {
1106 1.9.2.2 tron union twa_response_queue rq;
1107 1.9.2.2 tron uint32_t status_reg;
1108 1.9.2.2 tron
1109 1.9.2.2 tron for (;;) {
1110 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1111 1.9.2.2 tron if (twa_check_ctlr_state(sc, status_reg))
1112 1.9.2.2 tron return(1);
1113 1.9.2.2 tron if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1114 1.9.2.2 tron return(0); /* no more response queue entries */
1115 1.9.2.2 tron rq = (union twa_response_queue)twa_inl(sc,
1116 1.9.2.2 tron TWA_RESPONSE_QUEUE_OFFSET);
1117 1.9.2.2 tron }
1118 1.9.2.2 tron }
1119 1.9.2.2 tron
1120 1.9.2.2 tron static void
1121 1.9.2.2 tron twa_drain_busy_queue(struct twa_softc *sc)
1122 1.9.2.2 tron {
1123 1.9.2.2 tron struct twa_request *tr;
1124 1.9.2.2 tron
1125 1.9.2.2 tron /* Walk the busy queue. */
1126 1.9.2.2 tron
1127 1.9.2.2 tron while ((tr = TAILQ_FIRST(&sc->twa_busy)) != NULL) {
1128 1.9.2.2 tron TAILQ_REMOVE(&sc->twa_busy, tr, tr_link);
1129 1.9.2.2 tron
1130 1.9.2.2 tron twa_unmap_request(tr);
1131 1.9.2.2 tron if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_INTERNAL) ||
1132 1.9.2.2 tron (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_IOCTL)) {
1133 1.9.2.2 tron /* It's an internal/ioctl request. Simply free it. */
1134 1.9.2.2 tron if (tr->tr_data)
1135 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
1136 1.9.2.2 tron twa_release_request(tr);
1137 1.9.2.2 tron } else {
1138 1.9.2.2 tron /* It's a SCSI request. Complete it. */
1139 1.9.2.2 tron tr->tr_command->command.cmd_pkt_9k.status = EIO;
1140 1.9.2.2 tron if (tr->tr_callback)
1141 1.9.2.2 tron tr->tr_callback(tr);
1142 1.9.2.2 tron }
1143 1.9.2.2 tron }
1144 1.9.2.2 tron }
1145 1.9.2.2 tron
1146 1.9.2.2 tron static int
1147 1.9.2.2 tron twa_drain_pending_queue(struct twa_softc *sc)
1148 1.9.2.2 tron {
1149 1.9.2.2 tron struct twa_request *tr;
1150 1.9.2.2 tron int s, error = 0;
1151 1.9.2.2 tron
1152 1.9.2.2 tron /*
1153 1.9.2.2 tron * Pull requests off the pending queue, and submit them.
1154 1.9.2.2 tron */
1155 1.9.2.2 tron s = splbio();
1156 1.9.2.2 tron while ((tr = TAILQ_FIRST(&sc->twa_pending)) != NULL) {
1157 1.9.2.2 tron TAILQ_REMOVE(&sc->twa_pending, tr, tr_link);
1158 1.9.2.2 tron
1159 1.9.2.2 tron if ((error = twa_start(tr))) {
1160 1.9.2.2 tron if (error == EBUSY) {
1161 1.9.2.2 tron tr->tr_status = TWA_CMD_PENDING;
1162 1.9.2.2 tron
1163 1.9.2.2 tron /* queue at the head */
1164 1.9.2.2 tron TAILQ_INSERT_HEAD(&tr->tr_sc->twa_pending,
1165 1.9.2.2 tron tr, tr_link);
1166 1.9.2.2 tron error = 0;
1167 1.9.2.2 tron break;
1168 1.9.2.2 tron } else {
1169 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_SLEEP_ON_REQUEST) {
1170 1.9.2.2 tron tr->tr_error = error;
1171 1.9.2.2 tron tr->tr_callback(tr);
1172 1.9.2.2 tron error = EIO;
1173 1.9.2.2 tron }
1174 1.9.2.2 tron }
1175 1.9.2.2 tron }
1176 1.9.2.2 tron }
1177 1.9.2.2 tron splx(s);
1178 1.9.2.2 tron
1179 1.9.2.2 tron return(error);
1180 1.9.2.2 tron }
1181 1.9.2.2 tron
1182 1.9.2.2 tron static int
1183 1.9.2.2 tron twa_drain_aen_queue(struct twa_softc *sc)
1184 1.9.2.2 tron {
1185 1.9.2.2 tron int s, error = 0;
1186 1.9.2.2 tron struct twa_request *tr;
1187 1.9.2.2 tron struct twa_command_header *cmd_hdr;
1188 1.9.2.2 tron struct timeval t1;
1189 1.9.2.2 tron uint32_t timeout;
1190 1.9.2.2 tron
1191 1.9.2.2 tron for (;;) {
1192 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL) {
1193 1.9.2.2 tron error = EIO;
1194 1.9.2.2 tron break;
1195 1.9.2.2 tron }
1196 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1197 1.9.2.2 tron tr->tr_callback = NULL;
1198 1.9.2.2 tron
1199 1.9.2.2 tron tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
1200 1.9.2.2 tron
1201 1.9.2.2 tron if (tr->tr_data == NULL) {
1202 1.9.2.2 tron error = 1;
1203 1.9.2.2 tron goto out;
1204 1.9.2.2 tron }
1205 1.9.2.2 tron
1206 1.9.2.2 tron if (twa_request_sense(tr, 0) != 0) {
1207 1.9.2.2 tron error = 1;
1208 1.9.2.2 tron break;
1209 1.9.2.2 tron }
1210 1.9.2.2 tron
1211 1.9.2.2 tron timeout = (1000/*ms*/ * 100/*us*/ * TWA_REQUEST_TIMEOUT_PERIOD);
1212 1.9.2.2 tron
1213 1.9.2.2 tron microtime(&t1);
1214 1.9.2.2 tron
1215 1.9.2.2 tron timeout += t1.tv_usec;
1216 1.9.2.2 tron
1217 1.9.2.2 tron do {
1218 1.9.2.2 tron s = splbio();
1219 1.9.2.2 tron twa_done(tr->tr_sc);
1220 1.9.2.2 tron splx(s);
1221 1.9.2.2 tron if (tr->tr_status != TWA_CMD_BUSY)
1222 1.9.2.2 tron break;
1223 1.9.2.2 tron microtime(&t1);
1224 1.9.2.2 tron } while (t1.tv_usec <= timeout);
1225 1.9.2.2 tron
1226 1.9.2.2 tron if (tr->tr_status != TWA_CMD_COMPLETE) {
1227 1.9.2.2 tron error = ETIMEDOUT;
1228 1.9.2.2 tron break;
1229 1.9.2.2 tron }
1230 1.9.2.2 tron
1231 1.9.2.2 tron if ((error = tr->tr_command->command.cmd_pkt_9k.status))
1232 1.9.2.2 tron break;
1233 1.9.2.2 tron
1234 1.9.2.2 tron cmd_hdr = (struct twa_command_header *)(tr->tr_data);
1235 1.9.2.2 tron if ((cmd_hdr->status_block.error) /* aen_code */
1236 1.9.2.2 tron == TWA_AEN_QUEUE_EMPTY)
1237 1.9.2.2 tron break;
1238 1.9.2.2 tron (void)twa_enqueue_aen(sc, cmd_hdr);
1239 1.9.2.2 tron
1240 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
1241 1.9.2.2 tron twa_release_request(tr);
1242 1.9.2.2 tron }
1243 1.9.2.2 tron out:
1244 1.9.2.2 tron if (tr) {
1245 1.9.2.2 tron if (tr->tr_data)
1246 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
1247 1.9.2.2 tron
1248 1.9.2.2 tron twa_release_request(tr);
1249 1.9.2.2 tron }
1250 1.9.2.2 tron return(error);
1251 1.9.2.2 tron }
1252 1.9.2.2 tron
1253 1.9.2.2 tron
1254 1.9.2.2 tron #ifdef DIAGNOSTIC
1255 1.9.2.2 tron static void
1256 1.9.2.2 tron twa_check_response_q(struct twa_request *tr, int clear)
1257 1.9.2.2 tron {
1258 1.9.2.2 tron int j;
1259 1.9.2.2 tron static int i = 0;
1260 1.9.2.2 tron static struct twa_request *req = 0;
1261 1.9.2.2 tron static struct buf *hist[255];
1262 1.9.2.2 tron
1263 1.9.2.2 tron
1264 1.9.2.2 tron if (clear) {
1265 1.9.2.2 tron i = 0;
1266 1.9.2.2 tron for (j = 0; j < 255; j++)
1267 1.9.2.2 tron hist[j] = 0;
1268 1.9.2.2 tron return;
1269 1.9.2.2 tron }
1270 1.9.2.2 tron
1271 1.9.2.2 tron if (req == 0)
1272 1.9.2.2 tron req = tr;
1273 1.9.2.2 tron
1274 1.9.2.2 tron if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_EXTERNAL) != 0) {
1275 1.9.2.2 tron if (req->tr_request_id == tr->tr_request_id)
1276 1.9.2.2 tron panic("req id: %d on controller queue twice",
1277 1.9.2.2 tron tr->tr_request_id);
1278 1.9.2.2 tron
1279 1.9.2.2 tron for (j = 0; j < i; j++)
1280 1.9.2.2 tron if (tr->bp == hist[j])
1281 1.9.2.2 tron panic("req id: %d buf found twice",
1282 1.9.2.2 tron tr->tr_request_id);
1283 1.9.2.2 tron }
1284 1.9.2.2 tron req = tr;
1285 1.9.2.2 tron
1286 1.9.2.2 tron hist[i++] = req->bp;
1287 1.9.2.2 tron }
1288 1.9.2.2 tron #endif
1289 1.9.2.2 tron
1290 1.9.2.2 tron static int
1291 1.9.2.2 tron twa_done(struct twa_softc *sc)
1292 1.9.2.2 tron {
1293 1.9.2.2 tron union twa_response_queue rq;
1294 1.9.2.2 tron struct twa_request *tr;
1295 1.9.2.2 tron int rv = 0;
1296 1.9.2.2 tron uint32_t status_reg;
1297 1.9.2.2 tron
1298 1.9.2.2 tron for (;;) {
1299 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
1300 1.9.2.2 tron if ((rv = twa_check_ctlr_state(sc, status_reg)))
1301 1.9.2.2 tron break;
1302 1.9.2.2 tron if (status_reg & TWA_STATUS_RESPONSE_QUEUE_EMPTY)
1303 1.9.2.2 tron break;
1304 1.9.2.2 tron /* Response queue is not empty. */
1305 1.9.2.2 tron rq = (union twa_response_queue)twa_inl(sc,
1306 1.9.2.2 tron TWA_RESPONSE_QUEUE_OFFSET);
1307 1.9.2.2 tron tr = sc->sc_twa_request + rq.u.response_id;
1308 1.9.2.2 tron #ifdef DIAGNOSTIC
1309 1.9.2.2 tron twa_check_response_q(tr, 0);
1310 1.9.2.2 tron #endif
1311 1.9.2.2 tron /* Unmap the command packet, and any associated data buffer. */
1312 1.9.2.2 tron twa_unmap_request(tr);
1313 1.9.2.2 tron
1314 1.9.2.2 tron tr->tr_status = TWA_CMD_COMPLETE;
1315 1.9.2.2 tron TAILQ_REMOVE(&tr->tr_sc->twa_busy, tr, tr_link);
1316 1.9.2.2 tron
1317 1.9.2.2 tron if (tr->tr_callback)
1318 1.9.2.2 tron tr->tr_callback(tr);
1319 1.9.2.2 tron }
1320 1.9.2.2 tron (void)twa_drain_pending_queue(sc);
1321 1.9.2.2 tron
1322 1.9.2.2 tron #ifdef DIAGNOSTIC
1323 1.9.2.2 tron twa_check_response_q(NULL, 1);
1324 1.9.2.2 tron #endif
1325 1.9.2.2 tron return(rv);
1326 1.9.2.2 tron }
1327 1.9.2.2 tron
1328 1.9.2.2 tron /*
1329 1.9.2.2 tron * Function name: twa_init_ctlr
1330 1.9.2.2 tron * Description: Establishes a logical connection with the controller.
1331 1.9.2.2 tron * If bundled with firmware, determines whether or not
1332 1.9.2.2 tron * to flash firmware, based on arch_id, fw SRL (Spec.
1333 1.9.2.2 tron * Revision Level), branch & build #'s. Also determines
1334 1.9.2.2 tron * whether or not the driver is compatible with the
1335 1.9.2.2 tron * firmware on the controller, before proceeding to work
1336 1.9.2.2 tron * with it.
1337 1.9.2.2 tron *
1338 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
1339 1.9.2.2 tron * Output: None
1340 1.9.2.2 tron * Return value: 0 -- success
1341 1.9.2.2 tron * non-zero-- failure
1342 1.9.2.2 tron */
1343 1.9.2.2 tron static int
1344 1.9.2.2 tron twa_init_ctlr(struct twa_softc *sc)
1345 1.9.2.2 tron {
1346 1.9.2.2 tron uint16_t fw_on_ctlr_srl = 0;
1347 1.9.2.2 tron uint16_t fw_on_ctlr_arch_id = 0;
1348 1.9.2.2 tron uint16_t fw_on_ctlr_branch = 0;
1349 1.9.2.2 tron uint16_t fw_on_ctlr_build = 0;
1350 1.9.2.2 tron uint32_t init_connect_result = 0;
1351 1.9.2.2 tron int error = 0;
1352 1.9.2.2 tron #if 0
1353 1.9.2.2 tron int8_t fw_flashed = FALSE;
1354 1.9.2.2 tron int8_t fw_flash_failed = FALSE;
1355 1.9.2.2 tron #endif
1356 1.9.2.2 tron
1357 1.9.2.2 tron /* Wait for the controller to become ready. */
1358 1.9.2.2 tron if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY,
1359 1.9.2.2 tron TWA_REQUEST_TIMEOUT_PERIOD)) {
1360 1.9.2.2 tron return(ENXIO);
1361 1.9.2.2 tron }
1362 1.9.2.2 tron /* Drain the response queue. */
1363 1.9.2.2 tron if (twa_drain_response_queue(sc))
1364 1.9.2.2 tron return(1);
1365 1.9.2.2 tron
1366 1.9.2.2 tron /* Establish a logical connection with the controller. */
1367 1.9.2.2 tron if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
1368 1.9.2.2 tron TWA_EXTENDED_INIT_CONNECT, TWA_CURRENT_FW_SRL,
1369 1.9.2.2 tron TWA_9000_ARCH_ID, TWA_CURRENT_FW_BRANCH,
1370 1.9.2.2 tron TWA_CURRENT_FW_BUILD, &fw_on_ctlr_srl,
1371 1.9.2.2 tron &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1372 1.9.2.2 tron &fw_on_ctlr_build, &init_connect_result))) {
1373 1.9.2.2 tron return(error);
1374 1.9.2.2 tron }
1375 1.9.2.2 tron #if 0
1376 1.9.2.2 tron if ((init_connect_result & TWA_BUNDLED_FW_SAFE_TO_FLASH) &&
1377 1.9.2.2 tron (init_connect_result & TWA_CTLR_FW_RECOMMENDS_FLASH)) {
1378 1.9.2.2 tron /*
1379 1.9.2.2 tron * The bundled firmware is safe to flash, and the firmware
1380 1.9.2.2 tron * on the controller recommends a flash. So, flash!
1381 1.9.2.2 tron */
1382 1.9.2.2 tron printf("%s: flashing bundled firmware...\n",
1383 1.9.2.2 tron sc->twa_dv.dv_xname);
1384 1.9.2.2 tron
1385 1.9.2.2 tron if ((error = twa_flash_firmware(sc))) {
1386 1.9.2.2 tron fw_flash_failed = TRUE;
1387 1.9.2.2 tron
1388 1.9.2.2 tron printf("%s: unable to flash bundled firmware.\n",
1389 1.9.2.2 tron sc->twa_dv.dv_xname);
1390 1.9.2.2 tron } else {
1391 1.9.2.2 tron printf("%s: successfully flashed bundled firmware.\n",
1392 1.9.2.2 tron sc->twa_dv.dv_xname);
1393 1.9.2.2 tron fw_flashed = TRUE;
1394 1.9.2.2 tron }
1395 1.9.2.2 tron }
1396 1.9.2.2 tron if (fw_flashed) {
1397 1.9.2.2 tron /* The firmware was flashed. Have the new image loaded */
1398 1.9.2.2 tron error = twa_hard_reset(sc);
1399 1.9.2.2 tron if (error == 0)
1400 1.9.2.2 tron error = twa_init_ctlr(sc);
1401 1.9.2.2 tron /*
1402 1.9.2.2 tron * If hard reset of controller failed, we need to return.
1403 1.9.2.2 tron * Otherwise, the above recursive call to twa_init_ctlr will
1404 1.9.2.2 tron * have completed the rest of the initialization (starting
1405 1.9.2.2 tron * from twa_drain_aen_queue below). Don't do it again.
1406 1.9.2.2 tron * Just return.
1407 1.9.2.2 tron */
1408 1.9.2.2 tron return(error);
1409 1.9.2.2 tron } else {
1410 1.9.2.2 tron /*
1411 1.9.2.2 tron * Either we are not bundled with a firmware image, or
1412 1.9.2.2 tron * the bundled firmware is not safe to flash,
1413 1.9.2.2 tron * or flash failed for some reason. See if we can at
1414 1.9.2.2 tron * least work with the firmware on the controller in the
1415 1.9.2.2 tron * current mode.
1416 1.9.2.2 tron */
1417 1.9.2.2 tron if (init_connect_result & TWA_CTLR_FW_COMPATIBLE) {
1418 1.9.2.2 tron /* Yes, we can. Make note of the operating mode. */
1419 1.9.2.2 tron sc->working_srl = TWA_CURRENT_FW_SRL;
1420 1.9.2.2 tron sc->working_branch = TWA_CURRENT_FW_BRANCH;
1421 1.9.2.2 tron sc->working_build = TWA_CURRENT_FW_BUILD;
1422 1.9.2.2 tron } else {
1423 1.9.2.2 tron /*
1424 1.9.2.2 tron * No, we can't. See if we can at least work with
1425 1.9.2.2 tron * it in the base mode. We should never come here
1426 1.9.2.2 tron * if firmware has just been flashed.
1427 1.9.2.2 tron */
1428 1.9.2.2 tron printf("%s: Driver/Firmware mismatch. Negotiating "
1429 1.9.2.2 tron "for base level.\n", sc->twa_dv.dv_xname);
1430 1.9.2.2 tron if ((error = twa_init_connection(sc,
1431 1.9.2.2 tron TWA_INIT_MESSAGE_CREDITS,
1432 1.9.2.2 tron TWA_EXTENDED_INIT_CONNECT, TWA_BASE_FW_SRL,
1433 1.9.2.2 tron TWA_9000_ARCH_ID, TWA_BASE_FW_BRANCH,
1434 1.9.2.2 tron TWA_BASE_FW_BUILD, &fw_on_ctlr_srl,
1435 1.9.2.2 tron &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1436 1.9.2.2 tron &fw_on_ctlr_build, &init_connect_result))) {
1437 1.9.2.2 tron printf("%s: can't initialize connection in "
1438 1.9.2.2 tron "base mode.\n", sc->twa_dv.dv_xname);
1439 1.9.2.2 tron return(error);
1440 1.9.2.2 tron }
1441 1.9.2.2 tron if (!(init_connect_result & TWA_CTLR_FW_COMPATIBLE)) {
1442 1.9.2.2 tron /*
1443 1.9.2.2 tron * The firmware on the controller is not even
1444 1.9.2.2 tron * compatible with our base mode. We cannot
1445 1.9.2.2 tron * work with it. Bail...
1446 1.9.2.2 tron */
1447 1.9.2.2 tron printf("Incompatible firmware on controller\n");
1448 1.9.2.2 tron #ifdef TWA_FLASH_FIRMWARE
1449 1.9.2.2 tron if (fw_flash_failed)
1450 1.9.2.2 tron printf("...and could not flash bundled "
1451 1.9.2.2 tron "firmware.\n");
1452 1.9.2.2 tron else
1453 1.9.2.2 tron printf("...and bundled firmware not "
1454 1.9.2.2 tron "safe to flash.\n");
1455 1.9.2.2 tron #endif /* TWA_FLASH_FIRMWARE */
1456 1.9.2.2 tron return(1);
1457 1.9.2.2 tron }
1458 1.9.2.2 tron /*
1459 1.9.2.2 tron * We can work with this firmware, but only in
1460 1.9.2.2 tron * base mode.
1461 1.9.2.2 tron */
1462 1.9.2.2 tron sc->working_srl = TWA_BASE_FW_SRL;
1463 1.9.2.2 tron sc->working_branch = TWA_BASE_FW_BRANCH;
1464 1.9.2.2 tron sc->working_build = TWA_BASE_FW_BUILD;
1465 1.9.2.2 tron sc->twa_operating_mode = TWA_BASE_MODE;
1466 1.9.2.2 tron }
1467 1.9.2.2 tron }
1468 1.9.2.2 tron #endif
1469 1.9.2.2 tron twa_drain_aen_queue(sc);
1470 1.9.2.2 tron
1471 1.9.2.2 tron /* Set controller state to initialized. */
1472 1.9.2.2 tron sc->twa_state &= ~TWA_STATE_SHUTDOWN;
1473 1.9.2.2 tron return(0);
1474 1.9.2.2 tron }
1475 1.9.2.2 tron
1476 1.9.2.2 tron static int
1477 1.9.2.2 tron twa_setup(struct twa_softc *sc)
1478 1.9.2.2 tron {
1479 1.9.2.2 tron struct tw_cl_event_packet *aen_queue;
1480 1.9.2.2 tron uint32_t i = 0;
1481 1.9.2.2 tron int error = 0;
1482 1.9.2.2 tron
1483 1.9.2.2 tron /* Initialize request queues. */
1484 1.9.2.2 tron TAILQ_INIT(&sc->twa_free);
1485 1.9.2.2 tron TAILQ_INIT(&sc->twa_busy);
1486 1.9.2.2 tron TAILQ_INIT(&sc->twa_pending);
1487 1.9.2.2 tron
1488 1.9.2.2 tron sc->sc_nunits = 0;
1489 1.9.2.2 tron sc->twa_sc_flags = 0;
1490 1.9.2.2 tron
1491 1.9.2.2 tron if (twa_alloc_req_pkts(sc, TWA_Q_LENGTH)) {
1492 1.9.2.2 tron
1493 1.9.2.2 tron return(ENOMEM);
1494 1.9.2.2 tron }
1495 1.9.2.2 tron
1496 1.9.2.2 tron /* Allocate memory for the AEN queue. */
1497 1.9.2.2 tron if ((aen_queue = malloc(sizeof(struct tw_cl_event_packet) *
1498 1.9.2.2 tron TWA_Q_LENGTH, M_DEVBUF, M_WAITOK)) == NULL) {
1499 1.9.2.2 tron /*
1500 1.9.2.2 tron * This should not cause us to return error. We will only be
1501 1.9.2.2 tron * unable to support AEN's. But then, we will have to check
1502 1.9.2.2 tron * time and again to see if we can support AEN's, if we
1503 1.9.2.2 tron * continue. So, we will just return error.
1504 1.9.2.2 tron */
1505 1.9.2.2 tron return (ENOMEM);
1506 1.9.2.2 tron }
1507 1.9.2.2 tron /* Initialize the aen queue. */
1508 1.9.2.2 tron memset(aen_queue, 0, sizeof(struct tw_cl_event_packet) * TWA_Q_LENGTH);
1509 1.9.2.2 tron
1510 1.9.2.2 tron for (i = 0; i < TWA_Q_LENGTH; i++)
1511 1.9.2.2 tron sc->twa_aen_queue[i] = &(aen_queue[i]);
1512 1.9.2.2 tron
1513 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1514 1.9.2.2 tron TWA_CONTROL_DISABLE_INTERRUPTS);
1515 1.9.2.2 tron
1516 1.9.2.2 tron /* Initialize the controller. */
1517 1.9.2.2 tron if ((error = twa_init_ctlr(sc))) {
1518 1.9.2.2 tron /* Soft reset the controller, and try one more time. */
1519 1.9.2.2 tron
1520 1.9.2.2 tron printf("%s: controller initialization failed. "
1521 1.9.2.2 tron "Retrying initialization\n", sc->twa_dv.dv_xname);
1522 1.9.2.2 tron
1523 1.9.2.2 tron if ((error = twa_soft_reset(sc)) == 0)
1524 1.9.2.2 tron error = twa_init_ctlr(sc);
1525 1.9.2.2 tron }
1526 1.9.2.2 tron
1527 1.9.2.2 tron twa_describe_controller(sc);
1528 1.9.2.2 tron
1529 1.9.2.2 tron error = twa_request_bus_scan(sc);
1530 1.9.2.2 tron
1531 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1532 1.9.2.2 tron TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
1533 1.9.2.2 tron TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
1534 1.9.2.2 tron TWA_CONTROL_ENABLE_INTERRUPTS);
1535 1.9.2.2 tron
1536 1.9.2.2 tron return (error);
1537 1.9.2.2 tron }
1538 1.9.2.2 tron
1539 1.9.2.2 tron void *twa_sdh;
1540 1.9.2.2 tron
1541 1.9.2.2 tron static void
1542 1.9.2.2 tron twa_attach(struct device *parent, struct device *self, void *aux)
1543 1.9.2.2 tron {
1544 1.9.2.2 tron struct pci_attach_args *pa;
1545 1.9.2.2 tron struct twa_softc *sc;
1546 1.9.2.2 tron pci_chipset_tag_t pc;
1547 1.9.2.2 tron pcireg_t csr;
1548 1.9.2.2 tron pci_intr_handle_t ih;
1549 1.9.2.2 tron const char *intrstr;
1550 1.9.2.2 tron
1551 1.9.2.2 tron sc = (struct twa_softc *)self;
1552 1.9.2.2 tron
1553 1.9.2.2 tron pa = aux;
1554 1.9.2.2 tron pc = pa->pa_pc;
1555 1.9.2.2 tron sc->pc = pa->pa_pc;
1556 1.9.2.2 tron sc->tag = pa->pa_tag;
1557 1.9.2.2 tron sc->twa_dma_tag = pa->pa_dmat;
1558 1.9.2.2 tron
1559 1.9.2.2 tron aprint_naive(": RAID controller\n");
1560 1.9.2.2 tron aprint_normal(": 3ware Apache\n");
1561 1.9.2.2 tron
1562 1.9.2.2 tron if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9000) {
1563 1.9.2.2 tron if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
1564 1.9.2.2 tron &sc->twa_bus_iot, &sc->twa_bus_ioh, NULL, NULL)) {
1565 1.9.2.2 tron aprint_error("%s: can't map i/o space\n",
1566 1.9.2.2 tron sc->twa_dv.dv_xname);
1567 1.9.2.2 tron return;
1568 1.9.2.2 tron }
1569 1.9.2.2 tron } else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_9550) {
1570 1.9.2.2 tron if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x08,
1571 1.9.2.2 tron PCI_MAPREG_MEM_TYPE_64BIT, 0, &sc->twa_bus_iot,
1572 1.9.2.2 tron &sc->twa_bus_ioh, NULL, NULL)) {
1573 1.9.2.2 tron aprint_error("%s: can't map mem space\n",
1574 1.9.2.2 tron sc->twa_dv.dv_xname);
1575 1.9.2.2 tron return;
1576 1.9.2.2 tron }
1577 1.9.2.2 tron } else {
1578 1.9.2.2 tron aprint_error("%s: product id 0x%02x not recognized\n",
1579 1.9.2.2 tron sc->twa_dv.dv_xname, PCI_PRODUCT(pa->pa_id));
1580 1.9.2.2 tron return;
1581 1.9.2.2 tron }
1582 1.9.2.2 tron /* Enable the device. */
1583 1.9.2.2 tron csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1584 1.9.2.2 tron
1585 1.9.2.2 tron pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1586 1.9.2.2 tron csr | PCI_COMMAND_MASTER_ENABLE);
1587 1.9.2.2 tron
1588 1.9.2.2 tron /* Map and establish the interrupt. */
1589 1.9.2.2 tron if (pci_intr_map(pa, &ih)) {
1590 1.9.2.2 tron aprint_error("%s: can't map interrupt\n", sc->twa_dv.dv_xname);
1591 1.9.2.2 tron return;
1592 1.9.2.2 tron }
1593 1.9.2.2 tron intrstr = pci_intr_string(pc, ih);
1594 1.9.2.2 tron
1595 1.9.2.2 tron sc->twa_ih = pci_intr_establish(pc, ih, IPL_BIO, twa_intr, sc);
1596 1.9.2.2 tron if (sc->twa_ih == NULL) {
1597 1.9.2.2 tron aprint_error("%s: can't establish interrupt%s%s\n",
1598 1.9.2.2 tron sc->twa_dv.dv_xname,
1599 1.9.2.2 tron (intrstr) ? " at " : "",
1600 1.9.2.2 tron (intrstr) ? intrstr : "");
1601 1.9.2.2 tron return;
1602 1.9.2.2 tron }
1603 1.9.2.2 tron
1604 1.9.2.2 tron if (intrstr != NULL)
1605 1.9.2.2 tron aprint_normal("%s: interrupting at %s\n",
1606 1.9.2.2 tron sc->twa_dv.dv_xname, intrstr);
1607 1.9.2.2 tron
1608 1.9.2.2 tron twa_setup(sc);
1609 1.9.2.2 tron
1610 1.9.2.2 tron if (twa_sdh == NULL)
1611 1.9.2.2 tron twa_sdh = shutdownhook_establish(twa_shutdown, NULL);
1612 1.9.2.2 tron
1613 1.9.2.2 tron return;
1614 1.9.2.2 tron }
1615 1.9.2.2 tron
1616 1.9.2.2 tron static void
1617 1.9.2.2 tron twa_shutdown(void *arg)
1618 1.9.2.2 tron {
1619 1.9.2.2 tron extern struct cfdriver twa_cd;
1620 1.9.2.2 tron struct twa_softc *sc;
1621 1.9.2.2 tron int i, rv, unit;
1622 1.9.2.2 tron
1623 1.9.2.2 tron for (i = 0; i < twa_cd.cd_ndevs; i++) {
1624 1.9.2.2 tron if ((sc = device_lookup(&twa_cd, i)) == NULL)
1625 1.9.2.2 tron continue;
1626 1.9.2.2 tron
1627 1.9.2.2 tron for (unit = 0; unit < TWA_MAX_UNITS; unit++)
1628 1.9.2.2 tron if (sc->sc_units[unit].td_dev != NULL)
1629 1.9.2.2 tron (void) config_detach(sc->sc_units[unit].td_dev,
1630 1.9.2.2 tron DETACH_FORCE | DETACH_QUIET);
1631 1.9.2.2 tron
1632 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
1633 1.9.2.2 tron TWA_CONTROL_DISABLE_INTERRUPTS);
1634 1.9.2.2 tron
1635 1.9.2.2 tron /* Let the controller know that we are going down. */
1636 1.9.2.2 tron rv = twa_init_connection(sc, TWA_SHUTDOWN_MESSAGE_CREDITS,
1637 1.9.2.2 tron 0, 0, 0, 0, 0,
1638 1.9.2.2 tron NULL, NULL, NULL, NULL, NULL);
1639 1.9.2.2 tron }
1640 1.9.2.2 tron }
1641 1.9.2.2 tron
1642 1.9.2.2 tron void
1643 1.9.2.2 tron twa_register_callbacks(struct twa_softc *sc, int unit,
1644 1.9.2.2 tron const struct twa_callbacks *tcb)
1645 1.9.2.2 tron {
1646 1.9.2.2 tron
1647 1.9.2.2 tron sc->sc_units[unit].td_callbacks = tcb;
1648 1.9.2.2 tron }
1649 1.9.2.2 tron
1650 1.9.2.2 tron /*
1651 1.9.2.2 tron * Print autoconfiguration message for a sub-device
1652 1.9.2.2 tron */
1653 1.9.2.2 tron static int
1654 1.9.2.2 tron twa_print(void *aux, const char *pnp)
1655 1.9.2.2 tron {
1656 1.9.2.2 tron struct twa_attach_args *twaa;
1657 1.9.2.2 tron
1658 1.9.2.2 tron twaa = aux;
1659 1.9.2.2 tron
1660 1.9.2.2 tron if (pnp !=NULL)
1661 1.9.2.2 tron aprint_normal("block device at %s\n", pnp);
1662 1.9.2.2 tron aprint_normal(" unit %d\n", twaa->twaa_unit);
1663 1.9.2.2 tron return (UNCONF);
1664 1.9.2.2 tron }
1665 1.9.2.2 tron
1666 1.9.2.2 tron static void
1667 1.9.2.2 tron twa_fillin_sgl(struct twa_sg *sgl, bus_dma_segment_t *segs, int nsegments)
1668 1.9.2.2 tron {
1669 1.9.2.2 tron int i;
1670 1.9.2.2 tron for (i = 0; i < nsegments; i++) {
1671 1.9.2.2 tron sgl[i].address = segs[i].ds_addr;
1672 1.9.2.2 tron sgl[i].length = (uint32_t)(segs[i].ds_len);
1673 1.9.2.2 tron }
1674 1.9.2.2 tron }
1675 1.9.2.2 tron
1676 1.9.2.2 tron static int
1677 1.9.2.2 tron twa_submit_io(struct twa_request *tr)
1678 1.9.2.2 tron {
1679 1.9.2.2 tron int error;
1680 1.9.2.2 tron
1681 1.9.2.2 tron if ((error = twa_start(tr))) {
1682 1.9.2.2 tron if (error == EBUSY)
1683 1.9.2.2 tron error = 0; /* request is in the pending queue */
1684 1.9.2.2 tron else {
1685 1.9.2.2 tron tr->tr_error = error;
1686 1.9.2.2 tron }
1687 1.9.2.2 tron }
1688 1.9.2.2 tron return(error);
1689 1.9.2.2 tron }
1690 1.9.2.2 tron
1691 1.9.2.2 tron /*
1692 1.9.2.2 tron * Function name: twa_setup_data_dmamap
1693 1.9.2.2 tron * Description: Callback of bus_dmamap_load for the buffer associated
1694 1.9.2.2 tron * with data. Updates the cmd pkt (size/sgl_entries
1695 1.9.2.2 tron * fields, as applicable) to reflect the number of sg
1696 1.9.2.2 tron * elements.
1697 1.9.2.2 tron *
1698 1.9.2.2 tron * Input: arg -- ptr to request pkt
1699 1.9.2.2 tron * segs -- ptr to a list of segment descriptors
1700 1.9.2.2 tron * nsegments--# of segments
1701 1.9.2.2 tron * error -- 0 if no errors encountered before callback,
1702 1.9.2.2 tron * non-zero if errors were encountered
1703 1.9.2.2 tron * Output: None
1704 1.9.2.2 tron * Return value: None
1705 1.9.2.2 tron */
1706 1.9.2.2 tron static int
1707 1.9.2.2 tron twa_setup_data_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments,
1708 1.9.2.2 tron int error)
1709 1.9.2.2 tron {
1710 1.9.2.2 tron struct twa_request *tr = (struct twa_request *)arg;
1711 1.9.2.2 tron struct twa_command_packet *cmdpkt = tr->tr_command;
1712 1.9.2.2 tron struct twa_command_9k *cmd9k;
1713 1.9.2.2 tron union twa_command_7k *cmd7k;
1714 1.9.2.2 tron uint8_t sgl_offset;
1715 1.9.2.2 tron
1716 1.9.2.2 tron if (error == EFBIG) {
1717 1.9.2.2 tron tr->tr_error = error;
1718 1.9.2.2 tron goto out;
1719 1.9.2.2 tron }
1720 1.9.2.2 tron
1721 1.9.2.2 tron if (tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) {
1722 1.9.2.2 tron cmd9k = &(cmdpkt->command.cmd_pkt_9k);
1723 1.9.2.2 tron twa_fillin_sgl(&(cmd9k->sg_list[0]), segs, nsegments);
1724 1.9.2.2 tron cmd9k->sgl_entries += nsegments - 1;
1725 1.9.2.2 tron } else {
1726 1.9.2.2 tron /* It's a 7000 command packet. */
1727 1.9.2.2 tron cmd7k = &(cmdpkt->command.cmd_pkt_7k);
1728 1.9.2.2 tron if ((sgl_offset = cmdpkt->command.cmd_pkt_7k.generic.sgl_offset))
1729 1.9.2.2 tron twa_fillin_sgl((struct twa_sg *)
1730 1.9.2.2 tron (((uint32_t *)cmd7k) + sgl_offset),
1731 1.9.2.2 tron segs, nsegments);
1732 1.9.2.2 tron /* Modify the size field, based on sg address size. */
1733 1.9.2.2 tron cmd7k->generic.size +=
1734 1.9.2.2 tron ((TWA_64BIT_ADDRESSES ? 3 : 2) * nsegments);
1735 1.9.2.2 tron }
1736 1.9.2.2 tron
1737 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_IN)
1738 1.9.2.2 tron bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1739 1.9.2.2 tron tr->tr_length, BUS_DMASYNC_PREREAD);
1740 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_OUT) {
1741 1.9.2.2 tron /*
1742 1.9.2.2 tron * If we're using an alignment buffer, and we're
1743 1.9.2.2 tron * writing data, copy the real data out.
1744 1.9.2.2 tron */
1745 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED)
1746 1.9.2.2 tron memcpy(tr->tr_data, tr->tr_real_data,
1747 1.9.2.2 tron tr->tr_real_length);
1748 1.9.2.2 tron bus_dmamap_sync(tr->tr_sc->twa_dma_tag, tr->tr_dma_map, 0,
1749 1.9.2.2 tron tr->tr_length, BUS_DMASYNC_PREWRITE);
1750 1.9.2.2 tron }
1751 1.9.2.2 tron error = twa_submit_io(tr);
1752 1.9.2.2 tron
1753 1.9.2.2 tron out:
1754 1.9.2.2 tron if (error) {
1755 1.9.2.2 tron twa_unmap_request(tr);
1756 1.9.2.2 tron /*
1757 1.9.2.2 tron * If the caller had been returned EINPROGRESS, and he has
1758 1.9.2.2 tron * registered a callback for handling completion, the callback
1759 1.9.2.2 tron * will never get called because we were unable to submit the
1760 1.9.2.2 tron * request. So, free up the request right here.
1761 1.9.2.2 tron */
1762 1.9.2.2 tron if ((tr->tr_flags & TWA_CMD_IN_PROGRESS) && (tr->tr_callback))
1763 1.9.2.2 tron twa_release_request(tr);
1764 1.9.2.2 tron }
1765 1.9.2.2 tron return (error);
1766 1.9.2.2 tron }
1767 1.9.2.2 tron
1768 1.9.2.2 tron /*
1769 1.9.2.2 tron * Function name: twa_map_request
1770 1.9.2.2 tron * Description: Maps a cmd pkt and data associated with it, into
1771 1.9.2.2 tron * DMA'able memory.
1772 1.9.2.2 tron *
1773 1.9.2.2 tron * Input: tr -- ptr to request pkt
1774 1.9.2.2 tron * Output: None
1775 1.9.2.2 tron * Return value: 0 -- success
1776 1.9.2.2 tron * non-zero-- failure
1777 1.9.2.2 tron */
1778 1.9.2.2 tron int
1779 1.9.2.2 tron twa_map_request(struct twa_request *tr)
1780 1.9.2.2 tron {
1781 1.9.2.2 tron struct twa_softc *sc = tr->tr_sc;
1782 1.9.2.2 tron int s, rv, error = 0;
1783 1.9.2.2 tron
1784 1.9.2.2 tron /* If the command involves data, map that too. */
1785 1.9.2.2 tron if (tr->tr_data != NULL) {
1786 1.9.2.2 tron
1787 1.9.2.2 tron if (((u_long)tr->tr_data & (511)) != 0) {
1788 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_COPY_NEEDED;
1789 1.9.2.2 tron tr->tr_real_data = tr->tr_data;
1790 1.9.2.2 tron tr->tr_real_length = tr->tr_length;
1791 1.9.2.2 tron s = splvm();
1792 1.9.2.2 tron tr->tr_data = (void *)uvm_km_kmemalloc1(kmem_map,
1793 1.9.2.2 tron NULL, tr->tr_length, 512, UVM_UNKNOWN_OFFSET,
1794 1.9.2.2 tron UVM_KMF_NOWAIT);
1795 1.9.2.2 tron splx(s);
1796 1.9.2.2 tron
1797 1.9.2.2 tron if (tr->tr_data == NULL) {
1798 1.9.2.2 tron tr->tr_data = tr->tr_real_data;
1799 1.9.2.2 tron tr->tr_length = tr->tr_real_length;
1800 1.9.2.2 tron return(ENOMEM);
1801 1.9.2.2 tron }
1802 1.9.2.2 tron if ((tr->tr_flags & TWA_CMD_DATA_IN) != 0)
1803 1.9.2.2 tron memcpy(tr->tr_data, tr->tr_real_data,
1804 1.9.2.2 tron tr->tr_length);
1805 1.9.2.2 tron }
1806 1.9.2.2 tron
1807 1.9.2.2 tron /*
1808 1.9.2.2 tron * Map the data buffer into bus space and build the S/G list.
1809 1.9.2.2 tron */
1810 1.9.2.2 tron rv = bus_dmamap_load(sc->twa_dma_tag, tr->tr_dma_map,
1811 1.9.2.2 tron tr->tr_data, tr->tr_length, NULL, BUS_DMA_NOWAIT |
1812 1.9.2.2 tron BUS_DMA_STREAMING | (tr->tr_flags & TWA_CMD_DATA_OUT) ?
1813 1.9.2.2 tron BUS_DMA_READ : BUS_DMA_WRITE);
1814 1.9.2.2 tron
1815 1.9.2.2 tron if (rv != 0) {
1816 1.9.2.2 tron if ((tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) != 0) {
1817 1.9.2.2 tron s = splvm();
1818 1.9.2.2 tron uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1819 1.9.2.2 tron tr->tr_length);
1820 1.9.2.2 tron splx(s);
1821 1.9.2.2 tron }
1822 1.9.2.2 tron return (rv);
1823 1.9.2.2 tron }
1824 1.9.2.2 tron
1825 1.9.2.2 tron if ((rv = twa_setup_data_dmamap(tr,
1826 1.9.2.2 tron tr->tr_dma_map->dm_segs,
1827 1.9.2.2 tron tr->tr_dma_map->dm_nsegs, error))) {
1828 1.9.2.2 tron
1829 1.9.2.2 tron if (tr->tr_flags & TWA_CMD_DATA_COPY_NEEDED) {
1830 1.9.2.2 tron s = splvm();
1831 1.9.2.2 tron uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1832 1.9.2.2 tron tr->tr_length);
1833 1.9.2.2 tron splx(s);
1834 1.9.2.2 tron tr->tr_data = tr->tr_real_data;
1835 1.9.2.2 tron tr->tr_length = tr->tr_real_length;
1836 1.9.2.2 tron }
1837 1.9.2.2 tron } else
1838 1.9.2.2 tron error = tr->tr_error;
1839 1.9.2.2 tron
1840 1.9.2.2 tron } else
1841 1.9.2.2 tron if ((rv = twa_submit_io(tr)))
1842 1.9.2.2 tron twa_unmap_request(tr);
1843 1.9.2.2 tron
1844 1.9.2.2 tron return (rv);
1845 1.9.2.2 tron }
1846 1.9.2.2 tron
1847 1.9.2.2 tron #if 0
1848 1.9.2.2 tron /*
1849 1.9.2.2 tron * Function name: twa_flash_firmware
1850 1.9.2.2 tron * Description: Flashes bundled firmware image onto controller.
1851 1.9.2.2 tron *
1852 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
1853 1.9.2.2 tron * Output: None
1854 1.9.2.2 tron * Return value: 0 -- success
1855 1.9.2.2 tron * non-zero-- failure
1856 1.9.2.2 tron */
1857 1.9.2.2 tron static int
1858 1.9.2.2 tron twa_flash_firmware(struct twa_softc *sc)
1859 1.9.2.2 tron {
1860 1.9.2.2 tron struct twa_request *tr;
1861 1.9.2.2 tron struct twa_command_download_firmware *cmd;
1862 1.9.2.2 tron uint32_t count;
1863 1.9.2.2 tron uint32_t fw_img_chunk_size;
1864 1.9.2.2 tron uint32_t this_chunk_size = 0;
1865 1.9.2.2 tron uint32_t remaining_img_size = 0;
1866 1.9.2.2 tron int s, error = 0;
1867 1.9.2.2 tron int i;
1868 1.9.2.2 tron
1869 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL) {
1870 1.9.2.2 tron /* No free request packets available. Can't proceed. */
1871 1.9.2.2 tron error = EIO;
1872 1.9.2.2 tron goto out;
1873 1.9.2.2 tron }
1874 1.9.2.2 tron
1875 1.9.2.2 tron count = (twa_fw_img_size / 65536);
1876 1.9.2.2 tron
1877 1.9.2.2 tron count += ((twa_fw_img_size % 65536) != 0) ? 1 : 0;
1878 1.9.2.2 tron
1879 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1880 1.9.2.2 tron /* Allocate sufficient memory to hold a chunk of the firmware image. */
1881 1.9.2.2 tron fw_img_chunk_size = ((twa_fw_img_size / count) + 511) & ~511;
1882 1.9.2.2 tron
1883 1.9.2.2 tron s = splvm();
1884 1.9.2.2 tron tr->tr_data = (void *)uvm_km_kmemalloc1(kmem_map, NULL,
1885 1.9.2.2 tron fw_img_chunk_size, 512, UVM_UNKNOWN_OFFSET, UVM_KMF_NOWAIT);
1886 1.9.2.2 tron splx(s);
1887 1.9.2.2 tron
1888 1.9.2.2 tron if (tr->tr_data == NULL) {
1889 1.9.2.2 tron error = ENOMEM;
1890 1.9.2.2 tron goto out;
1891 1.9.2.2 tron }
1892 1.9.2.2 tron
1893 1.9.2.2 tron remaining_img_size = twa_fw_img_size;
1894 1.9.2.2 tron cmd = &(tr->tr_command->command.cmd_pkt_7k.download_fw);
1895 1.9.2.2 tron
1896 1.9.2.2 tron for (i = 0; i < count; i++) {
1897 1.9.2.2 tron /* Build a cmd pkt for downloading firmware. */
1898 1.9.2.2 tron memset(tr->tr_command, 0, sizeof(struct twa_command_packet));
1899 1.9.2.2 tron
1900 1.9.2.2 tron tr->tr_command->cmd_hdr.header_desc.size_header = 128;
1901 1.9.2.2 tron
1902 1.9.2.2 tron cmd->opcode = TWA_OP_DOWNLOAD_FIRMWARE;
1903 1.9.2.2 tron cmd->sgl_offset = 2; /* offset in dwords, to the beginning
1904 1.9.2.2 tron of sg list */
1905 1.9.2.2 tron cmd->size = 2; /* this field will be updated at data
1906 1.9.2.2 tron map time */
1907 1.9.2.2 tron cmd->request_id = tr->tr_request_id;
1908 1.9.2.2 tron cmd->unit = 0;
1909 1.9.2.2 tron cmd->status = 0;
1910 1.9.2.2 tron cmd->flags = 0;
1911 1.9.2.2 tron cmd->param = 8; /* prom image */
1912 1.9.2.2 tron
1913 1.9.2.2 tron if (i != (count - 1))
1914 1.9.2.2 tron this_chunk_size = fw_img_chunk_size;
1915 1.9.2.2 tron else /* last chunk */
1916 1.9.2.2 tron this_chunk_size = remaining_img_size;
1917 1.9.2.2 tron
1918 1.9.2.2 tron remaining_img_size -= this_chunk_size;
1919 1.9.2.2 tron
1920 1.9.2.2 tron memset(tr->tr_data, fw_img_chunk_size, 0);
1921 1.9.2.2 tron
1922 1.9.2.2 tron memcpy(tr->tr_data, twa_fw_img + (i * fw_img_chunk_size),
1923 1.9.2.2 tron this_chunk_size);
1924 1.9.2.2 tron /*
1925 1.9.2.2 tron * The next line will effect only the last chunk.
1926 1.9.2.2 tron */
1927 1.9.2.2 tron tr->tr_length = (this_chunk_size + 511) & ~511;
1928 1.9.2.2 tron
1929 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_OUT;
1930 1.9.2.2 tron
1931 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
1932 1.9.2.2 tron
1933 1.9.2.2 tron if (error) {
1934 1.9.2.2 tron if (error == ETIMEDOUT)
1935 1.9.2.2 tron /* clean-up done by twa_immediate_request */
1936 1.9.2.2 tron return(error);
1937 1.9.2.2 tron break;
1938 1.9.2.2 tron }
1939 1.9.2.2 tron error = cmd->status;
1940 1.9.2.2 tron
1941 1.9.2.2 tron if (i != (count - 1)) {
1942 1.9.2.2 tron
1943 1.9.2.2 tron /*
1944 1.9.2.2 tron * XXX FreeBSD code doesn't check for no error condition
1945 1.9.2.2 tron * but based on observation, error seems to return 0
1946 1.9.2.2 tron */
1947 1.9.2.2 tron if ((error =
1948 1.9.2.2 tron tr->tr_command->cmd_hdr.status_block.error) == 0) {
1949 1.9.2.2 tron continue;
1950 1.9.2.2 tron } else if ((error =
1951 1.9.2.2 tron tr->tr_command->cmd_hdr.status_block.error) ==
1952 1.9.2.2 tron TWA_ERROR_MORE_DATA) {
1953 1.9.2.2 tron continue;
1954 1.9.2.2 tron } else {
1955 1.9.2.2 tron twa_hard_reset(sc);
1956 1.9.2.2 tron break;
1957 1.9.2.2 tron }
1958 1.9.2.2 tron } else /* last chunk */
1959 1.9.2.2 tron if (error) {
1960 1.9.2.2 tron printf("%s: firmware flash request failed. "
1961 1.9.2.2 tron "error = 0x%x\n", sc->twa_dv.dv_xname,
1962 1.9.2.2 tron error);
1963 1.9.2.2 tron twa_hard_reset(sc);
1964 1.9.2.2 tron }
1965 1.9.2.2 tron }
1966 1.9.2.2 tron
1967 1.9.2.2 tron if (tr->tr_data) {
1968 1.9.2.2 tron s = splvm();
1969 1.9.2.2 tron uvm_km_free(kmem_map, (vaddr_t)tr->tr_data,
1970 1.9.2.2 tron fw_img_chunk_size);
1971 1.9.2.2 tron splx(s);
1972 1.9.2.2 tron }
1973 1.9.2.2 tron out:
1974 1.9.2.2 tron if (tr)
1975 1.9.2.2 tron twa_release_request(tr);
1976 1.9.2.2 tron return(error);
1977 1.9.2.2 tron }
1978 1.9.2.2 tron
1979 1.9.2.2 tron /*
1980 1.9.2.2 tron * Function name: twa_hard_reset
1981 1.9.2.2 tron * Description: Hard reset the controller.
1982 1.9.2.2 tron *
1983 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
1984 1.9.2.2 tron * Output: None
1985 1.9.2.2 tron * Return value: 0 -- success
1986 1.9.2.2 tron * non-zero-- failure
1987 1.9.2.2 tron */
1988 1.9.2.2 tron static int
1989 1.9.2.2 tron twa_hard_reset(struct twa_softc *sc)
1990 1.9.2.2 tron {
1991 1.9.2.2 tron struct twa_request *tr;
1992 1.9.2.2 tron struct twa_command_reset_firmware *cmd;
1993 1.9.2.2 tron int error;
1994 1.9.2.2 tron
1995 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL)
1996 1.9.2.2 tron return(EIO);
1997 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
1998 1.9.2.2 tron /* Build a cmd pkt for sending down the hard reset command. */
1999 1.9.2.2 tron tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2000 1.9.2.2 tron
2001 1.9.2.2 tron cmd = &(tr->tr_command->command.cmd_pkt_7k.reset_fw);
2002 1.9.2.2 tron cmd->opcode = TWA_OP_RESET_FIRMWARE;
2003 1.9.2.2 tron cmd->size = 2; /* this field will be updated at data map time */
2004 1.9.2.2 tron cmd->request_id = tr->tr_request_id;
2005 1.9.2.2 tron cmd->unit = 0;
2006 1.9.2.2 tron cmd->status = 0;
2007 1.9.2.2 tron cmd->flags = 0;
2008 1.9.2.2 tron cmd->param = 0; /* don't reload FPGA logic */
2009 1.9.2.2 tron
2010 1.9.2.2 tron tr->tr_data = NULL;
2011 1.9.2.2 tron tr->tr_length = 0;
2012 1.9.2.2 tron
2013 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2014 1.9.2.2 tron if (error) {
2015 1.9.2.2 tron printf("%s: hard reset request could not be posted. "
2016 1.9.2.2 tron "error = 0x%x\n", sc->twa_dv.dv_xname, error);
2017 1.9.2.2 tron if (error == ETIMEDOUT)
2018 1.9.2.2 tron /* clean-up done by twa_immediate_request */
2019 1.9.2.2 tron return(error);
2020 1.9.2.2 tron goto out;
2021 1.9.2.2 tron }
2022 1.9.2.2 tron if ((error = cmd->status)) {
2023 1.9.2.2 tron printf("%s: hard reset request failed. error = 0x%x\n",
2024 1.9.2.2 tron sc->twa_dv.dv_xname, error);
2025 1.9.2.2 tron }
2026 1.9.2.2 tron
2027 1.9.2.2 tron out:
2028 1.9.2.2 tron if (tr)
2029 1.9.2.2 tron twa_release_request(tr);
2030 1.9.2.2 tron return(error);
2031 1.9.2.2 tron }
2032 1.9.2.2 tron #endif
2033 1.9.2.2 tron
2034 1.9.2.2 tron /*
2035 1.9.2.2 tron * Function name: twa_intr
2036 1.9.2.2 tron * Description: Interrupt handler. Determines the kind of interrupt,
2037 1.9.2.2 tron * and calls the appropriate handler.
2038 1.9.2.2 tron *
2039 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2040 1.9.2.2 tron * Output: None
2041 1.9.2.2 tron * Return value: None
2042 1.9.2.2 tron */
2043 1.9.2.2 tron
2044 1.9.2.2 tron static int
2045 1.9.2.2 tron twa_intr(void *arg)
2046 1.9.2.2 tron {
2047 1.9.2.2 tron int caught, s, rv;
2048 1.9.2.2 tron struct twa_softc *sc;
2049 1.9.2.2 tron uint32_t status_reg;
2050 1.9.2.2 tron sc = (struct twa_softc *)arg;
2051 1.9.2.2 tron
2052 1.9.2.2 tron caught = 0;
2053 1.9.2.2 tron /* Collect current interrupt status. */
2054 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2055 1.9.2.2 tron if (twa_check_ctlr_state(sc, status_reg)) {
2056 1.9.2.2 tron caught = 1;
2057 1.9.2.2 tron goto bail;
2058 1.9.2.2 tron }
2059 1.9.2.2 tron /* Dispatch based on the kind of interrupt. */
2060 1.9.2.2 tron if (status_reg & TWA_STATUS_HOST_INTERRUPT) {
2061 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2062 1.9.2.2 tron TWA_CONTROL_CLEAR_HOST_INTERRUPT);
2063 1.9.2.2 tron caught = 1;
2064 1.9.2.2 tron }
2065 1.9.2.2 tron if ((status_reg & TWA_STATUS_ATTENTION_INTERRUPT) != 0) {
2066 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2067 1.9.2.2 tron TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2068 1.9.2.2 tron rv = twa_fetch_aen(sc);
2069 1.9.2.2 tron #ifdef DIAGNOSTIC
2070 1.9.2.2 tron if (rv != 0)
2071 1.9.2.2 tron printf("%s: unable to retrieve AEN (%d)\n",
2072 1.9.2.2 tron sc->twa_dv.dv_xname, rv);
2073 1.9.2.2 tron #endif
2074 1.9.2.2 tron caught = 1;
2075 1.9.2.2 tron }
2076 1.9.2.2 tron if (status_reg & TWA_STATUS_COMMAND_INTERRUPT) {
2077 1.9.2.2 tron /* Start any requests that might be in the pending queue. */
2078 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2079 1.9.2.2 tron TWA_CONTROL_MASK_COMMAND_INTERRUPT);
2080 1.9.2.2 tron (void)twa_drain_pending_queue(sc);
2081 1.9.2.2 tron caught = 1;
2082 1.9.2.2 tron }
2083 1.9.2.2 tron if (status_reg & TWA_STATUS_RESPONSE_INTERRUPT) {
2084 1.9.2.2 tron s = splbio();
2085 1.9.2.2 tron twa_done(sc);
2086 1.9.2.2 tron splx(s);
2087 1.9.2.2 tron caught = 1;
2088 1.9.2.2 tron }
2089 1.9.2.2 tron bail:
2090 1.9.2.2 tron return (caught);
2091 1.9.2.2 tron }
2092 1.9.2.2 tron
2093 1.9.2.2 tron /*
2094 1.9.2.2 tron * Accept an open operation on the control device.
2095 1.9.2.2 tron */
2096 1.9.2.2 tron static int
2097 1.9.2.2 tron twaopen(dev_t dev, int flag, int mode, struct proc *p)
2098 1.9.2.2 tron {
2099 1.9.2.2 tron struct twa_softc *twa;
2100 1.9.2.2 tron
2101 1.9.2.2 tron if ((twa = device_lookup(&twa_cd, minor(dev))) == NULL)
2102 1.9.2.2 tron return (ENXIO);
2103 1.9.2.2 tron if ((twa->twa_sc_flags & TWA_STATE_OPEN) != 0)
2104 1.9.2.2 tron return (EBUSY);
2105 1.9.2.2 tron
2106 1.9.2.2 tron twa->twa_sc_flags |= TWA_STATE_OPEN;
2107 1.9.2.2 tron
2108 1.9.2.2 tron return (0);
2109 1.9.2.2 tron }
2110 1.9.2.2 tron
2111 1.9.2.2 tron /*
2112 1.9.2.2 tron * Accept the last close on the control device.
2113 1.9.2.2 tron */
2114 1.9.2.2 tron static int
2115 1.9.2.2 tron twaclose(dev_t dev, int flag, int mode, struct proc *p)
2116 1.9.2.2 tron {
2117 1.9.2.2 tron struct twa_softc *twa;
2118 1.9.2.2 tron
2119 1.9.2.2 tron twa = device_lookup(&twa_cd, minor(dev));
2120 1.9.2.2 tron twa->twa_sc_flags &= ~TWA_STATE_OPEN;
2121 1.9.2.2 tron return (0);
2122 1.9.2.2 tron }
2123 1.9.2.2 tron
2124 1.9.2.2 tron /*
2125 1.9.2.2 tron * Function name: twaioctl
2126 1.9.2.2 tron * Description: ioctl handler.
2127 1.9.2.2 tron *
2128 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2129 1.9.2.2 tron * cmd -- ioctl cmd
2130 1.9.2.2 tron * buf -- ptr to buffer in kernel memory, which is
2131 1.9.2.2 tron * a copy of the input buffer in user-space
2132 1.9.2.2 tron * Output: buf -- ptr to buffer in kernel memory, which will
2133 1.9.2.2 tron * be copied of the output buffer in user-space
2134 1.9.2.2 tron * Return value: 0 -- success
2135 1.9.2.2 tron * non-zero-- failure
2136 1.9.2.2 tron */
2137 1.9.2.2 tron static int
2138 1.9.2.2 tron twaioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
2139 1.9.2.2 tron {
2140 1.9.2.2 tron struct twa_softc *sc;
2141 1.9.2.2 tron struct twa_ioctl_9k *user_buf = (struct twa_ioctl_9k *)data;
2142 1.9.2.2 tron struct tw_cl_event_packet event_buf;
2143 1.9.2.2 tron struct twa_request *tr = 0;
2144 1.9.2.2 tron int32_t event_index = 0;
2145 1.9.2.2 tron int32_t start_index;
2146 1.9.2.2 tron int s, error = 0;
2147 1.9.2.2 tron
2148 1.9.2.2 tron sc = device_lookup(&twa_cd, minor(dev));
2149 1.9.2.2 tron
2150 1.9.2.2 tron switch (cmd) {
2151 1.9.2.2 tron case TW_OSL_IOCTL_FIRMWARE_PASS_THROUGH:
2152 1.9.2.2 tron {
2153 1.9.2.2 tron struct twa_command_packet *cmdpkt;
2154 1.9.2.2 tron uint32_t data_buf_size_adjusted;
2155 1.9.2.2 tron
2156 1.9.2.2 tron /* Get a request packet */
2157 1.9.2.2 tron tr = twa_get_request_wait(sc, 0);
2158 1.9.2.2 tron KASSERT(tr != NULL);
2159 1.9.2.2 tron /*
2160 1.9.2.2 tron * Make sure that the data buffer sent to firmware is a
2161 1.9.2.2 tron * 512 byte multiple in size.
2162 1.9.2.2 tron */
2163 1.9.2.2 tron data_buf_size_adjusted =
2164 1.9.2.2 tron (user_buf->twa_drvr_pkt.buffer_length + 511) & ~511;
2165 1.9.2.2 tron
2166 1.9.2.2 tron if ((tr->tr_length = data_buf_size_adjusted)) {
2167 1.9.2.2 tron if ((tr->tr_data = malloc(data_buf_size_adjusted,
2168 1.9.2.2 tron M_DEVBUF, M_WAITOK)) == NULL) {
2169 1.9.2.2 tron error = ENOMEM;
2170 1.9.2.2 tron goto fw_passthru_done;
2171 1.9.2.2 tron }
2172 1.9.2.2 tron /* Copy the payload. */
2173 1.9.2.2 tron if ((error = copyin((void *) (user_buf->pdata),
2174 1.9.2.2 tron (void *) (tr->tr_data),
2175 1.9.2.2 tron user_buf->twa_drvr_pkt.buffer_length)) != 0) {
2176 1.9.2.2 tron goto fw_passthru_done;
2177 1.9.2.2 tron }
2178 1.9.2.2 tron tr->tr_flags |= TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2179 1.9.2.2 tron }
2180 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_IOCTL;
2181 1.9.2.2 tron cmdpkt = tr->tr_command;
2182 1.9.2.2 tron
2183 1.9.2.2 tron /* Copy the command packet. */
2184 1.9.2.2 tron memcpy(cmdpkt, &(user_buf->twa_cmd_pkt),
2185 1.9.2.2 tron sizeof(struct twa_command_packet));
2186 1.9.2.2 tron cmdpkt->command.cmd_pkt_7k.generic.request_id =
2187 1.9.2.2 tron tr->tr_request_id;
2188 1.9.2.2 tron
2189 1.9.2.2 tron /* Send down the request, and wait for it to complete. */
2190 1.9.2.2 tron if ((error = twa_wait_request(tr, TWA_REQUEST_TIMEOUT_PERIOD))) {
2191 1.9.2.2 tron if (error == ETIMEDOUT)
2192 1.9.2.2 tron break; /* clean-up done by twa_wait_request */
2193 1.9.2.2 tron goto fw_passthru_done;
2194 1.9.2.2 tron }
2195 1.9.2.2 tron
2196 1.9.2.2 tron /* Copy the command packet back into user space. */
2197 1.9.2.2 tron memcpy(&user_buf->twa_cmd_pkt, cmdpkt,
2198 1.9.2.2 tron sizeof(struct twa_command_packet));
2199 1.9.2.2 tron
2200 1.9.2.2 tron /* If there was a payload, copy it back too. */
2201 1.9.2.2 tron if (tr->tr_length)
2202 1.9.2.2 tron error = copyout(tr->tr_data, user_buf->pdata,
2203 1.9.2.2 tron user_buf->twa_drvr_pkt.buffer_length);
2204 1.9.2.2 tron fw_passthru_done:
2205 1.9.2.2 tron /* Free resources. */
2206 1.9.2.2 tron if (tr->tr_data)
2207 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
2208 1.9.2.2 tron
2209 1.9.2.2 tron if (tr)
2210 1.9.2.2 tron twa_release_request(tr);
2211 1.9.2.2 tron break;
2212 1.9.2.2 tron }
2213 1.9.2.2 tron
2214 1.9.2.2 tron case TW_OSL_IOCTL_SCAN_BUS:
2215 1.9.2.2 tron twa_request_bus_scan(sc);
2216 1.9.2.2 tron break;
2217 1.9.2.2 tron
2218 1.9.2.2 tron case TW_CL_IOCTL_GET_FIRST_EVENT:
2219 1.9.2.2 tron if (sc->twa_aen_queue_wrapped) {
2220 1.9.2.2 tron if (sc->twa_aen_queue_overflow) {
2221 1.9.2.2 tron /*
2222 1.9.2.2 tron * The aen queue has wrapped, even before some
2223 1.9.2.2 tron * events have been retrieved. Let the caller
2224 1.9.2.2 tron * know that he missed out on some AEN's.
2225 1.9.2.2 tron */
2226 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2227 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW;
2228 1.9.2.2 tron sc->twa_aen_queue_overflow = FALSE;
2229 1.9.2.2 tron } else
2230 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2231 1.9.2.2 tron event_index = sc->twa_aen_head;
2232 1.9.2.2 tron } else {
2233 1.9.2.2 tron if (sc->twa_aen_head == sc->twa_aen_tail) {
2234 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2235 1.9.2.2 tron TWA_ERROR_AEN_NO_EVENTS;
2236 1.9.2.2 tron break;
2237 1.9.2.2 tron }
2238 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2239 1.9.2.2 tron event_index = sc->twa_aen_tail; /* = 0 */
2240 1.9.2.2 tron }
2241 1.9.2.2 tron if ((error = copyout(sc->twa_aen_queue[event_index],
2242 1.9.2.2 tron user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2243 1.9.2.2 tron (sc->twa_aen_queue[event_index])->retrieved =
2244 1.9.2.2 tron TWA_AEN_RETRIEVED;
2245 1.9.2.2 tron break;
2246 1.9.2.2 tron
2247 1.9.2.2 tron case TW_CL_IOCTL_GET_LAST_EVENT:
2248 1.9.2.2 tron if (sc->twa_aen_queue_wrapped) {
2249 1.9.2.2 tron if (sc->twa_aen_queue_overflow) {
2250 1.9.2.2 tron /*
2251 1.9.2.2 tron * The aen queue has wrapped, even before some
2252 1.9.2.2 tron * events have been retrieved. Let the caller
2253 1.9.2.2 tron * know that he missed out on some AEN's.
2254 1.9.2.2 tron */
2255 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2256 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW;
2257 1.9.2.2 tron sc->twa_aen_queue_overflow = FALSE;
2258 1.9.2.2 tron } else
2259 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2260 1.9.2.2 tron } else {
2261 1.9.2.2 tron if (sc->twa_aen_head == sc->twa_aen_tail) {
2262 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2263 1.9.2.2 tron TWA_ERROR_AEN_NO_EVENTS;
2264 1.9.2.2 tron break;
2265 1.9.2.2 tron }
2266 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2267 1.9.2.2 tron }
2268 1.9.2.2 tron event_index =
2269 1.9.2.2 tron (sc->twa_aen_head - 1 + TWA_Q_LENGTH) % TWA_Q_LENGTH;
2270 1.9.2.2 tron if ((error = copyout(sc->twa_aen_queue[event_index],
2271 1.9.2.2 tron user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2272 1.9.2.2 tron (sc->twa_aen_queue[event_index])->retrieved =
2273 1.9.2.2 tron TWA_AEN_RETRIEVED;
2274 1.9.2.2 tron break;
2275 1.9.2.2 tron
2276 1.9.2.2 tron case TW_CL_IOCTL_GET_NEXT_EVENT:
2277 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2278 1.9.2.2 tron if (sc->twa_aen_queue_wrapped) {
2279 1.9.2.2 tron
2280 1.9.2.2 tron if (sc->twa_aen_queue_overflow) {
2281 1.9.2.2 tron /*
2282 1.9.2.2 tron * The aen queue has wrapped, even before some
2283 1.9.2.2 tron * events have been retrieved. Let the caller
2284 1.9.2.2 tron * know that he missed out on some AEN's.
2285 1.9.2.2 tron */
2286 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2287 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW;
2288 1.9.2.2 tron sc->twa_aen_queue_overflow = FALSE;
2289 1.9.2.2 tron }
2290 1.9.2.2 tron start_index = sc->twa_aen_head;
2291 1.9.2.2 tron } else {
2292 1.9.2.2 tron if (sc->twa_aen_head == sc->twa_aen_tail) {
2293 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2294 1.9.2.2 tron TWA_ERROR_AEN_NO_EVENTS;
2295 1.9.2.2 tron break;
2296 1.9.2.2 tron }
2297 1.9.2.2 tron start_index = sc->twa_aen_tail; /* = 0 */
2298 1.9.2.2 tron }
2299 1.9.2.2 tron error = copyin(user_buf->pdata, &event_buf,
2300 1.9.2.2 tron sizeof(struct tw_cl_event_packet));
2301 1.9.2.2 tron
2302 1.9.2.2 tron event_index = (start_index + event_buf.sequence_id -
2303 1.9.2.2 tron (sc->twa_aen_queue[start_index])->sequence_id + 1)
2304 1.9.2.2 tron % TWA_Q_LENGTH;
2305 1.9.2.2 tron
2306 1.9.2.2 tron if (!((sc->twa_aen_queue[event_index])->sequence_id >
2307 1.9.2.2 tron event_buf.sequence_id)) {
2308 1.9.2.2 tron if (user_buf->twa_drvr_pkt.status ==
2309 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW)
2310 1.9.2.2 tron /* so we report the overflow next time */
2311 1.9.2.2 tron sc->twa_aen_queue_overflow = TRUE;
2312 1.9.2.2 tron user_buf->twa_drvr_pkt.status = TWA_ERROR_AEN_NO_EVENTS;
2313 1.9.2.2 tron break;
2314 1.9.2.2 tron }
2315 1.9.2.2 tron if ((error = copyout(sc->twa_aen_queue[event_index],
2316 1.9.2.2 tron user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2317 1.9.2.2 tron (sc->twa_aen_queue[event_index])->retrieved =
2318 1.9.2.2 tron TWA_AEN_RETRIEVED;
2319 1.9.2.2 tron break;
2320 1.9.2.2 tron
2321 1.9.2.2 tron case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
2322 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2323 1.9.2.2 tron if (sc->twa_aen_queue_wrapped) {
2324 1.9.2.2 tron if (sc->twa_aen_queue_overflow) {
2325 1.9.2.2 tron /*
2326 1.9.2.2 tron * The aen queue has wrapped, even before some
2327 1.9.2.2 tron * events have been retrieved. Let the caller
2328 1.9.2.2 tron * know that he missed out on some AEN's.
2329 1.9.2.2 tron */
2330 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2331 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW;
2332 1.9.2.2 tron sc->twa_aen_queue_overflow = FALSE;
2333 1.9.2.2 tron }
2334 1.9.2.2 tron start_index = sc->twa_aen_head;
2335 1.9.2.2 tron } else {
2336 1.9.2.2 tron if (sc->twa_aen_head == sc->twa_aen_tail) {
2337 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2338 1.9.2.2 tron TWA_ERROR_AEN_NO_EVENTS;
2339 1.9.2.2 tron break;
2340 1.9.2.2 tron }
2341 1.9.2.2 tron start_index = sc->twa_aen_tail; /* = 0 */
2342 1.9.2.2 tron }
2343 1.9.2.2 tron if ((error = copyin(user_buf->pdata, &event_buf,
2344 1.9.2.2 tron sizeof(struct tw_cl_event_packet))) != 0)
2345 1.9.2.2 tron
2346 1.9.2.2 tron event_index = (start_index + event_buf.sequence_id -
2347 1.9.2.2 tron (sc->twa_aen_queue[start_index])->sequence_id - 1)
2348 1.9.2.2 tron % TWA_Q_LENGTH;
2349 1.9.2.2 tron if (!((sc->twa_aen_queue[event_index])->sequence_id <
2350 1.9.2.2 tron event_buf.sequence_id)) {
2351 1.9.2.2 tron if (user_buf->twa_drvr_pkt.status ==
2352 1.9.2.2 tron TWA_ERROR_AEN_OVERFLOW)
2353 1.9.2.2 tron /* so we report the overflow next time */
2354 1.9.2.2 tron sc->twa_aen_queue_overflow = TRUE;
2355 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2356 1.9.2.2 tron TWA_ERROR_AEN_NO_EVENTS;
2357 1.9.2.2 tron break;
2358 1.9.2.2 tron }
2359 1.9.2.2 tron if ((error = copyout(sc->twa_aen_queue [event_index],
2360 1.9.2.2 tron user_buf->pdata, sizeof(struct tw_cl_event_packet))) != 0)
2361 1.9.2.2 tron aprint_error("%s: get_previous: Could not copyout to "
2362 1.9.2.2 tron "event_buf. error = %x\n", sc->twa_dv.dv_xname,
2363 1.9.2.2 tron error);
2364 1.9.2.2 tron (sc->twa_aen_queue[event_index])->retrieved = TWA_AEN_RETRIEVED;
2365 1.9.2.2 tron break;
2366 1.9.2.2 tron
2367 1.9.2.2 tron case TW_CL_IOCTL_GET_LOCK:
2368 1.9.2.2 tron {
2369 1.9.2.2 tron struct tw_cl_lock_packet twa_lock;
2370 1.9.2.2 tron
2371 1.9.2.2 tron copyin(user_buf->pdata, &twa_lock,
2372 1.9.2.2 tron sizeof(struct tw_cl_lock_packet));
2373 1.9.2.2 tron s = splbio();
2374 1.9.2.2 tron if ((sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) ||
2375 1.9.2.2 tron (twa_lock.force_flag) ||
2376 1.9.2.2 tron (time.tv_sec >= sc->twa_ioctl_lock.timeout)) {
2377 1.9.2.2 tron
2378 1.9.2.2 tron sc->twa_ioctl_lock.lock = TWA_LOCK_HELD;
2379 1.9.2.2 tron sc->twa_ioctl_lock.timeout = time.tv_sec +
2380 1.9.2.2 tron (twa_lock.timeout_msec / 1000);
2381 1.9.2.2 tron twa_lock.time_remaining_msec = twa_lock.timeout_msec;
2382 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2383 1.9.2.2 tron } else {
2384 1.9.2.2 tron twa_lock.time_remaining_msec =
2385 1.9.2.2 tron (sc->twa_ioctl_lock.timeout - time.tv_sec) *
2386 1.9.2.2 tron 1000;
2387 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2388 1.9.2.2 tron TWA_ERROR_IOCTL_LOCK_ALREADY_HELD;
2389 1.9.2.2 tron }
2390 1.9.2.2 tron splx(s);
2391 1.9.2.2 tron copyout(&twa_lock, user_buf->pdata,
2392 1.9.2.2 tron sizeof(struct tw_cl_lock_packet));
2393 1.9.2.2 tron break;
2394 1.9.2.2 tron }
2395 1.9.2.2 tron
2396 1.9.2.2 tron case TW_CL_IOCTL_RELEASE_LOCK:
2397 1.9.2.2 tron s = splbio();
2398 1.9.2.2 tron if (sc->twa_ioctl_lock.lock == TWA_LOCK_FREE) {
2399 1.9.2.2 tron user_buf->twa_drvr_pkt.status =
2400 1.9.2.2 tron TWA_ERROR_IOCTL_LOCK_NOT_HELD;
2401 1.9.2.2 tron } else {
2402 1.9.2.2 tron sc->twa_ioctl_lock.lock = TWA_LOCK_FREE;
2403 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2404 1.9.2.2 tron }
2405 1.9.2.2 tron splx(s);
2406 1.9.2.2 tron break;
2407 1.9.2.2 tron
2408 1.9.2.2 tron case TW_CL_IOCTL_GET_COMPATIBILITY_INFO:
2409 1.9.2.2 tron {
2410 1.9.2.2 tron struct tw_cl_compatibility_packet comp_pkt;
2411 1.9.2.2 tron
2412 1.9.2.2 tron memcpy(comp_pkt.driver_version, TWA_DRIVER_VERSION_STRING,
2413 1.9.2.2 tron sizeof(TWA_DRIVER_VERSION_STRING));
2414 1.9.2.2 tron comp_pkt.working_srl = sc->working_srl;
2415 1.9.2.2 tron comp_pkt.working_branch = sc->working_branch;
2416 1.9.2.2 tron comp_pkt.working_build = sc->working_build;
2417 1.9.2.2 tron user_buf->twa_drvr_pkt.status = 0;
2418 1.9.2.2 tron
2419 1.9.2.2 tron /* Copy compatibility information to user space. */
2420 1.9.2.2 tron copyout(&comp_pkt, user_buf->pdata,
2421 1.9.2.2 tron min(sizeof(struct tw_cl_compatibility_packet),
2422 1.9.2.2 tron user_buf->twa_drvr_pkt.buffer_length));
2423 1.9.2.2 tron break;
2424 1.9.2.2 tron }
2425 1.9.2.2 tron
2426 1.9.2.2 tron case TWA_IOCTL_GET_UNITNAME: /* WASABI EXTENSION */
2427 1.9.2.2 tron {
2428 1.9.2.2 tron struct twa_unitname *tn;
2429 1.9.2.2 tron struct twa_drive *tdr;
2430 1.9.2.2 tron
2431 1.9.2.2 tron tn = (struct twa_unitname *)data;
2432 1.9.2.2 tron /* XXX mutex */
2433 1.9.2.2 tron if (tn->tn_unit < 0 || tn->tn_unit >= TWA_MAX_UNITS)
2434 1.9.2.2 tron return (EINVAL);
2435 1.9.2.2 tron tdr = &sc->sc_units[tn->tn_unit];
2436 1.9.2.2 tron if (tdr->td_dev == NULL)
2437 1.9.2.2 tron tn->tn_name[0] = '\0';
2438 1.9.2.2 tron else
2439 1.9.2.2 tron strlcpy(tn->tn_name, tdr->td_dev->dv_xname,
2440 1.9.2.2 tron sizeof(tn->tn_name));
2441 1.9.2.2 tron return (0);
2442 1.9.2.2 tron }
2443 1.9.2.2 tron
2444 1.9.2.2 tron default:
2445 1.9.2.2 tron /* Unknown opcode. */
2446 1.9.2.2 tron error = ENOTTY;
2447 1.9.2.2 tron }
2448 1.9.2.2 tron
2449 1.9.2.2 tron return(error);
2450 1.9.2.2 tron }
2451 1.9.2.2 tron
2452 1.9.2.2 tron const struct cdevsw twa_cdevsw = {
2453 1.9.2.2 tron twaopen, twaclose, noread, nowrite, twaioctl,
2454 1.9.2.2 tron nostop, notty, nopoll, nommap,
2455 1.9.2.2 tron };
2456 1.9.2.2 tron
2457 1.9.2.2 tron /*
2458 1.9.2.2 tron * Function name: twa_get_param
2459 1.9.2.2 tron * Description: Get a firmware parameter.
2460 1.9.2.2 tron *
2461 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2462 1.9.2.2 tron * table_id -- parameter table #
2463 1.9.2.2 tron * param_id -- index of the parameter in the table
2464 1.9.2.2 tron * param_size -- size of the parameter in bytes
2465 1.9.2.2 tron * callback -- ptr to function, if any, to be called
2466 1.9.2.2 tron * back on completion; NULL if no callback.
2467 1.9.2.2 tron * Output: None
2468 1.9.2.2 tron * Return value: ptr to param structure -- success
2469 1.9.2.2 tron * NULL -- failure
2470 1.9.2.2 tron */
2471 1.9.2.2 tron static int
2472 1.9.2.2 tron twa_get_param(struct twa_softc *sc, int table_id, int param_id,
2473 1.9.2.2 tron size_t param_size, void (* callback)(struct twa_request *tr),
2474 1.9.2.2 tron struct twa_param_9k **param)
2475 1.9.2.2 tron {
2476 1.9.2.2 tron int rv = 0;
2477 1.9.2.2 tron struct twa_request *tr;
2478 1.9.2.2 tron union twa_command_7k *cmd;
2479 1.9.2.2 tron
2480 1.9.2.2 tron /* Get a request packet. */
2481 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL) {
2482 1.9.2.2 tron rv = EAGAIN;
2483 1.9.2.2 tron goto out;
2484 1.9.2.2 tron }
2485 1.9.2.2 tron
2486 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2487 1.9.2.2 tron
2488 1.9.2.2 tron /* Allocate memory to read data into. */
2489 1.9.2.2 tron if ((*param = (struct twa_param_9k *)
2490 1.9.2.2 tron malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL) {
2491 1.9.2.2 tron rv = ENOMEM;
2492 1.9.2.2 tron goto out;
2493 1.9.2.2 tron }
2494 1.9.2.2 tron
2495 1.9.2.2 tron memset(*param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2496 1.9.2.2 tron tr->tr_data = *param;
2497 1.9.2.2 tron tr->tr_length = TWA_SECTOR_SIZE;
2498 1.9.2.2 tron tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2499 1.9.2.2 tron
2500 1.9.2.2 tron /* Build the cmd pkt. */
2501 1.9.2.2 tron cmd = &(tr->tr_command->command.cmd_pkt_7k);
2502 1.9.2.2 tron
2503 1.9.2.2 tron tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2504 1.9.2.2 tron
2505 1.9.2.2 tron cmd->param.opcode = TWA_OP_GET_PARAM;
2506 1.9.2.2 tron cmd->param.sgl_offset = 2;
2507 1.9.2.2 tron cmd->param.size = 2;
2508 1.9.2.2 tron cmd->param.request_id = tr->tr_request_id;
2509 1.9.2.2 tron cmd->param.unit = 0;
2510 1.9.2.2 tron cmd->param.param_count = 1;
2511 1.9.2.2 tron
2512 1.9.2.2 tron /* Specify which parameter we need. */
2513 1.9.2.2 tron (*param)->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2514 1.9.2.2 tron (*param)->parameter_id = param_id;
2515 1.9.2.2 tron (*param)->parameter_size_bytes = param_size;
2516 1.9.2.2 tron
2517 1.9.2.2 tron /* Submit the command. */
2518 1.9.2.2 tron if (callback == NULL) {
2519 1.9.2.2 tron /* There's no call back; wait till the command completes. */
2520 1.9.2.2 tron rv = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2521 1.9.2.2 tron
2522 1.9.2.2 tron if (rv != 0)
2523 1.9.2.2 tron goto out;
2524 1.9.2.2 tron
2525 1.9.2.2 tron if ((rv = cmd->param.status) != 0) {
2526 1.9.2.2 tron /* twa_drain_complete_queue will have done the unmapping */
2527 1.9.2.2 tron goto out;
2528 1.9.2.2 tron }
2529 1.9.2.2 tron twa_release_request(tr);
2530 1.9.2.2 tron return (rv);
2531 1.9.2.2 tron } else {
2532 1.9.2.2 tron /* There's a call back. Simply submit the command. */
2533 1.9.2.2 tron tr->tr_callback = callback;
2534 1.9.2.2 tron rv = twa_map_request(tr);
2535 1.9.2.2 tron return (rv);
2536 1.9.2.2 tron }
2537 1.9.2.2 tron out:
2538 1.9.2.2 tron if (tr)
2539 1.9.2.2 tron twa_release_request(tr);
2540 1.9.2.2 tron return(rv);
2541 1.9.2.2 tron }
2542 1.9.2.2 tron
2543 1.9.2.2 tron /*
2544 1.9.2.2 tron * Function name: twa_set_param
2545 1.9.2.2 tron * Description: Set a firmware parameter.
2546 1.9.2.2 tron *
2547 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2548 1.9.2.2 tron * table_id -- parameter table #
2549 1.9.2.2 tron * param_id -- index of the parameter in the table
2550 1.9.2.2 tron * param_size -- size of the parameter in bytes
2551 1.9.2.2 tron * callback -- ptr to function, if any, to be called
2552 1.9.2.2 tron * back on completion; NULL if no callback.
2553 1.9.2.2 tron * Output: None
2554 1.9.2.2 tron * Return value: 0 -- success
2555 1.9.2.2 tron * non-zero-- failure
2556 1.9.2.2 tron */
2557 1.9.2.2 tron static int
2558 1.9.2.2 tron twa_set_param(struct twa_softc *sc, int table_id, int param_id, int param_size,
2559 1.9.2.2 tron void *data, void (* callback)(struct twa_request *tr))
2560 1.9.2.2 tron {
2561 1.9.2.2 tron struct twa_request *tr;
2562 1.9.2.2 tron union twa_command_7k *cmd;
2563 1.9.2.2 tron struct twa_param_9k *param = NULL;
2564 1.9.2.2 tron int error = ENOMEM;
2565 1.9.2.2 tron
2566 1.9.2.2 tron tr = twa_get_request(sc, 0);
2567 1.9.2.2 tron if (tr == NULL)
2568 1.9.2.2 tron return (EAGAIN);
2569 1.9.2.2 tron
2570 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2571 1.9.2.2 tron
2572 1.9.2.2 tron /* Allocate memory to send data using. */
2573 1.9.2.2 tron if ((param = (struct twa_param_9k *)
2574 1.9.2.2 tron malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
2575 1.9.2.2 tron goto out;
2576 1.9.2.2 tron memset(param, 0, sizeof(struct twa_param_9k) - 1 + param_size);
2577 1.9.2.2 tron tr->tr_data = param;
2578 1.9.2.2 tron tr->tr_length = TWA_SECTOR_SIZE;
2579 1.9.2.2 tron tr->tr_flags = TWA_CMD_DATA_IN | TWA_CMD_DATA_OUT;
2580 1.9.2.2 tron
2581 1.9.2.2 tron /* Build the cmd pkt. */
2582 1.9.2.2 tron cmd = &(tr->tr_command->command.cmd_pkt_7k);
2583 1.9.2.2 tron
2584 1.9.2.2 tron tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2585 1.9.2.2 tron
2586 1.9.2.2 tron cmd->param.opcode = TWA_OP_SET_PARAM;
2587 1.9.2.2 tron cmd->param.sgl_offset = 2;
2588 1.9.2.2 tron cmd->param.size = 2;
2589 1.9.2.2 tron cmd->param.request_id = tr->tr_request_id;
2590 1.9.2.2 tron cmd->param.unit = 0;
2591 1.9.2.2 tron cmd->param.param_count = 1;
2592 1.9.2.2 tron
2593 1.9.2.2 tron /* Specify which parameter we want to set. */
2594 1.9.2.2 tron param->table_id = table_id | TWA_9K_PARAM_DESCRIPTOR;
2595 1.9.2.2 tron param->parameter_id = param_id;
2596 1.9.2.2 tron param->parameter_size_bytes = param_size;
2597 1.9.2.2 tron memcpy(param->data, data, param_size);
2598 1.9.2.2 tron
2599 1.9.2.2 tron /* Submit the command. */
2600 1.9.2.2 tron if (callback == NULL) {
2601 1.9.2.2 tron /* There's no call back; wait till the command completes. */
2602 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2603 1.9.2.2 tron if (error == ETIMEDOUT)
2604 1.9.2.2 tron /* clean-up done by twa_immediate_request */
2605 1.9.2.2 tron return(error);
2606 1.9.2.2 tron if (error)
2607 1.9.2.2 tron goto out;
2608 1.9.2.2 tron if ((error = cmd->param.status)) {
2609 1.9.2.2 tron /*
2610 1.9.2.2 tron * twa_drain_complete_queue will have done the
2611 1.9.2.2 tron * unmapping.
2612 1.9.2.2 tron */
2613 1.9.2.2 tron goto out;
2614 1.9.2.2 tron }
2615 1.9.2.2 tron free(param, M_DEVBUF);
2616 1.9.2.2 tron twa_release_request(tr);
2617 1.9.2.2 tron return(error);
2618 1.9.2.2 tron } else {
2619 1.9.2.2 tron /* There's a call back. Simply submit the command. */
2620 1.9.2.2 tron tr->tr_callback = callback;
2621 1.9.2.2 tron if ((error = twa_map_request(tr)))
2622 1.9.2.2 tron goto out;
2623 1.9.2.2 tron
2624 1.9.2.2 tron return (0);
2625 1.9.2.2 tron }
2626 1.9.2.2 tron out:
2627 1.9.2.2 tron if (param)
2628 1.9.2.2 tron free(param, M_DEVBUF);
2629 1.9.2.2 tron if (tr)
2630 1.9.2.2 tron twa_release_request(tr);
2631 1.9.2.2 tron return(error);
2632 1.9.2.2 tron }
2633 1.9.2.2 tron
2634 1.9.2.2 tron /*
2635 1.9.2.2 tron * Function name: twa_init_connection
2636 1.9.2.2 tron * Description: Send init_connection cmd to firmware
2637 1.9.2.2 tron *
2638 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2639 1.9.2.2 tron * message_credits -- max # of requests that we might send
2640 1.9.2.2 tron * down simultaneously. This will be
2641 1.9.2.2 tron * typically set to 256 at init-time or
2642 1.9.2.2 tron * after a reset, and to 1 at shutdown-time
2643 1.9.2.2 tron * set_features -- indicates if we intend to use 64-bit
2644 1.9.2.2 tron * sg, also indicates if we want to do a
2645 1.9.2.2 tron * basic or an extended init_connection;
2646 1.9.2.2 tron *
2647 1.9.2.2 tron * Note: The following input/output parameters are valid, only in case of an
2648 1.9.2.2 tron * extended init_connection:
2649 1.9.2.2 tron *
2650 1.9.2.2 tron * current_fw_srl -- srl of fw we are bundled
2651 1.9.2.2 tron * with, if any; 0 otherwise
2652 1.9.2.2 tron * current_fw_arch_id -- arch_id of fw we are bundled
2653 1.9.2.2 tron * with, if any; 0 otherwise
2654 1.9.2.2 tron * current_fw_branch -- branch # of fw we are bundled
2655 1.9.2.2 tron * with, if any; 0 otherwise
2656 1.9.2.2 tron * current_fw_build -- build # of fw we are bundled
2657 1.9.2.2 tron * with, if any; 0 otherwise
2658 1.9.2.2 tron * Output: fw_on_ctlr_srl -- srl of fw on ctlr
2659 1.9.2.2 tron * fw_on_ctlr_arch_id -- arch_id of fw on ctlr
2660 1.9.2.2 tron * fw_on_ctlr_branch -- branch # of fw on ctlr
2661 1.9.2.2 tron * fw_on_ctlr_build -- build # of fw on ctlr
2662 1.9.2.2 tron * init_connect_result -- result bitmap of fw response
2663 1.9.2.2 tron * Return value: 0 -- success
2664 1.9.2.2 tron * non-zero-- failure
2665 1.9.2.2 tron */
2666 1.9.2.2 tron static int
2667 1.9.2.2 tron twa_init_connection(struct twa_softc *sc, uint16_t message_credits,
2668 1.9.2.2 tron uint32_t set_features, uint16_t current_fw_srl,
2669 1.9.2.2 tron uint16_t current_fw_arch_id, uint16_t current_fw_branch,
2670 1.9.2.2 tron uint16_t current_fw_build, uint16_t *fw_on_ctlr_srl,
2671 1.9.2.2 tron uint16_t *fw_on_ctlr_arch_id, uint16_t *fw_on_ctlr_branch,
2672 1.9.2.2 tron uint16_t *fw_on_ctlr_build, uint32_t *init_connect_result)
2673 1.9.2.2 tron {
2674 1.9.2.2 tron struct twa_request *tr;
2675 1.9.2.2 tron struct twa_command_init_connect *init_connect;
2676 1.9.2.2 tron int error = 1;
2677 1.9.2.2 tron
2678 1.9.2.2 tron /* Get a request packet. */
2679 1.9.2.2 tron if ((tr = twa_get_request(sc, 0)) == NULL)
2680 1.9.2.2 tron goto out;
2681 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2682 1.9.2.2 tron /* Build the cmd pkt. */
2683 1.9.2.2 tron init_connect = &(tr->tr_command->command.cmd_pkt_7k.init_connect);
2684 1.9.2.2 tron
2685 1.9.2.2 tron tr->tr_command->cmd_hdr.header_desc.size_header = 128;
2686 1.9.2.2 tron
2687 1.9.2.2 tron init_connect->opcode = TWA_OP_INIT_CONNECTION;
2688 1.9.2.2 tron init_connect->request_id = tr->tr_request_id;
2689 1.9.2.2 tron init_connect->message_credits = message_credits;
2690 1.9.2.2 tron init_connect->features = set_features;
2691 1.9.2.2 tron if (TWA_64BIT_ADDRESSES) {
2692 1.9.2.2 tron printf("64 bit addressing supported for scatter/gather list\n");
2693 1.9.2.2 tron init_connect->features |= TWA_64BIT_SG_ADDRESSES;
2694 1.9.2.2 tron }
2695 1.9.2.2 tron if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2696 1.9.2.2 tron /*
2697 1.9.2.2 tron * Fill in the extra fields needed for
2698 1.9.2.2 tron * an extended init_connect.
2699 1.9.2.2 tron */
2700 1.9.2.2 tron init_connect->size = 6;
2701 1.9.2.2 tron init_connect->fw_srl = current_fw_srl;
2702 1.9.2.2 tron init_connect->fw_arch_id = current_fw_arch_id;
2703 1.9.2.2 tron init_connect->fw_branch = current_fw_branch;
2704 1.9.2.2 tron } else
2705 1.9.2.2 tron init_connect->size = 3;
2706 1.9.2.2 tron
2707 1.9.2.2 tron /* Submit the command, and wait for it to complete. */
2708 1.9.2.2 tron error = twa_immediate_request(tr, TWA_REQUEST_TIMEOUT_PERIOD);
2709 1.9.2.2 tron if (error == ETIMEDOUT)
2710 1.9.2.2 tron return(error); /* clean-up done by twa_immediate_request */
2711 1.9.2.2 tron if (error)
2712 1.9.2.2 tron goto out;
2713 1.9.2.2 tron if ((error = init_connect->status)) {
2714 1.9.2.2 tron /* twa_drain_complete_queue will have done the unmapping */
2715 1.9.2.2 tron goto out;
2716 1.9.2.2 tron }
2717 1.9.2.2 tron if (set_features & TWA_EXTENDED_INIT_CONNECT) {
2718 1.9.2.2 tron *fw_on_ctlr_srl = init_connect->fw_srl;
2719 1.9.2.2 tron *fw_on_ctlr_arch_id = init_connect->fw_arch_id;
2720 1.9.2.2 tron *fw_on_ctlr_branch = init_connect->fw_branch;
2721 1.9.2.2 tron *fw_on_ctlr_build = init_connect->fw_build;
2722 1.9.2.2 tron *init_connect_result = init_connect->result;
2723 1.9.2.2 tron }
2724 1.9.2.2 tron twa_release_request(tr);
2725 1.9.2.2 tron return(error);
2726 1.9.2.2 tron
2727 1.9.2.2 tron out:
2728 1.9.2.2 tron if (tr)
2729 1.9.2.2 tron twa_release_request(tr);
2730 1.9.2.2 tron return(error);
2731 1.9.2.2 tron }
2732 1.9.2.2 tron
2733 1.9.2.2 tron static int
2734 1.9.2.2 tron twa_reset(struct twa_softc *sc)
2735 1.9.2.2 tron {
2736 1.9.2.2 tron int s;
2737 1.9.2.2 tron int error = 0;
2738 1.9.2.2 tron
2739 1.9.2.2 tron /*
2740 1.9.2.2 tron * Disable interrupts from the controller, and mask any
2741 1.9.2.2 tron * accidental entry into our interrupt handler.
2742 1.9.2.2 tron */
2743 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2744 1.9.2.2 tron TWA_CONTROL_DISABLE_INTERRUPTS);
2745 1.9.2.2 tron
2746 1.9.2.2 tron s = splbio();
2747 1.9.2.2 tron
2748 1.9.2.2 tron /* Soft reset the controller. */
2749 1.9.2.2 tron if ((error = twa_soft_reset(sc)))
2750 1.9.2.2 tron goto out;
2751 1.9.2.2 tron
2752 1.9.2.2 tron /* Re-establish logical connection with the controller. */
2753 1.9.2.2 tron if ((error = twa_init_connection(sc, TWA_INIT_MESSAGE_CREDITS,
2754 1.9.2.2 tron 0, 0, 0, 0, 0,
2755 1.9.2.2 tron NULL, NULL, NULL, NULL, NULL))) {
2756 1.9.2.2 tron goto out;
2757 1.9.2.2 tron }
2758 1.9.2.2 tron /*
2759 1.9.2.2 tron * Complete all requests in the complete queue; error back all requests
2760 1.9.2.2 tron * in the busy queue. Any internal requests will be simply freed.
2761 1.9.2.2 tron * Re-submit any requests in the pending queue.
2762 1.9.2.2 tron */
2763 1.9.2.2 tron twa_drain_busy_queue(sc);
2764 1.9.2.2 tron
2765 1.9.2.2 tron out:
2766 1.9.2.2 tron splx(s);
2767 1.9.2.2 tron /*
2768 1.9.2.2 tron * Enable interrupts, and also clear attention and response interrupts.
2769 1.9.2.2 tron */
2770 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2771 1.9.2.2 tron TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2772 1.9.2.2 tron TWA_CONTROL_UNMASK_RESPONSE_INTERRUPT |
2773 1.9.2.2 tron TWA_CONTROL_ENABLE_INTERRUPTS);
2774 1.9.2.2 tron return(error);
2775 1.9.2.2 tron }
2776 1.9.2.2 tron
2777 1.9.2.2 tron static int
2778 1.9.2.2 tron twa_soft_reset(struct twa_softc *sc)
2779 1.9.2.2 tron {
2780 1.9.2.2 tron uint32_t status_reg;
2781 1.9.2.2 tron
2782 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2783 1.9.2.2 tron TWA_CONTROL_ISSUE_SOFT_RESET |
2784 1.9.2.2 tron TWA_CONTROL_CLEAR_HOST_INTERRUPT |
2785 1.9.2.2 tron TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT |
2786 1.9.2.2 tron TWA_CONTROL_MASK_COMMAND_INTERRUPT |
2787 1.9.2.2 tron TWA_CONTROL_MASK_RESPONSE_INTERRUPT |
2788 1.9.2.2 tron TWA_CONTROL_DISABLE_INTERRUPTS);
2789 1.9.2.2 tron
2790 1.9.2.2 tron if (twa_wait_status(sc, TWA_STATUS_MICROCONTROLLER_READY |
2791 1.9.2.2 tron TWA_STATUS_ATTENTION_INTERRUPT, 30)) {
2792 1.9.2.2 tron aprint_error("%s: no attention interrupt after reset.\n",
2793 1.9.2.2 tron sc->twa_dv.dv_xname);
2794 1.9.2.2 tron return(1);
2795 1.9.2.2 tron }
2796 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
2797 1.9.2.2 tron TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
2798 1.9.2.2 tron
2799 1.9.2.2 tron if (twa_drain_response_queue(sc)) {
2800 1.9.2.2 tron aprint_error("%s: cannot drain response queue.\n",
2801 1.9.2.2 tron sc->twa_dv.dv_xname);
2802 1.9.2.2 tron return(1);
2803 1.9.2.2 tron }
2804 1.9.2.2 tron if (twa_drain_aen_queue(sc)) {
2805 1.9.2.2 tron aprint_error("%s: cannot drain AEN queue.\n",
2806 1.9.2.2 tron sc->twa_dv.dv_xname);
2807 1.9.2.2 tron return(1);
2808 1.9.2.2 tron }
2809 1.9.2.2 tron if (twa_find_aen(sc, TWA_AEN_SOFT_RESET)) {
2810 1.9.2.2 tron aprint_error("%s: reset not reported by controller.\n",
2811 1.9.2.2 tron sc->twa_dv.dv_xname);
2812 1.9.2.2 tron return(1);
2813 1.9.2.2 tron }
2814 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2815 1.9.2.2 tron if (TWA_STATUS_ERRORS(status_reg) ||
2816 1.9.2.2 tron twa_check_ctlr_state(sc, status_reg)) {
2817 1.9.2.2 tron aprint_error("%s: controller errors detected.\n",
2818 1.9.2.2 tron sc->twa_dv.dv_xname);
2819 1.9.2.2 tron return(1);
2820 1.9.2.2 tron }
2821 1.9.2.2 tron return(0);
2822 1.9.2.2 tron }
2823 1.9.2.2 tron
2824 1.9.2.2 tron static int
2825 1.9.2.2 tron twa_wait_status(struct twa_softc *sc, uint32_t status, uint32_t timeout)
2826 1.9.2.2 tron {
2827 1.9.2.2 tron struct timeval t1;
2828 1.9.2.2 tron time_t end_time;
2829 1.9.2.2 tron uint32_t status_reg;
2830 1.9.2.2 tron
2831 1.9.2.2 tron timeout = (timeout * 1000 * 100);
2832 1.9.2.2 tron
2833 1.9.2.2 tron microtime(&t1);
2834 1.9.2.2 tron
2835 1.9.2.2 tron end_time = t1.tv_usec + timeout;
2836 1.9.2.2 tron
2837 1.9.2.2 tron do {
2838 1.9.2.2 tron status_reg = twa_inl(sc, TWA_STATUS_REGISTER_OFFSET);
2839 1.9.2.2 tron /* got the required bit(s)? */
2840 1.9.2.2 tron if ((status_reg & status) == status)
2841 1.9.2.2 tron return(0);
2842 1.9.2.2 tron DELAY(100000);
2843 1.9.2.2 tron microtime(&t1);
2844 1.9.2.2 tron } while (t1.tv_usec <= end_time);
2845 1.9.2.2 tron
2846 1.9.2.2 tron return(1);
2847 1.9.2.2 tron }
2848 1.9.2.2 tron
2849 1.9.2.2 tron static int
2850 1.9.2.2 tron twa_fetch_aen(struct twa_softc *sc)
2851 1.9.2.2 tron {
2852 1.9.2.2 tron struct twa_request *tr;
2853 1.9.2.2 tron int s, error = 0;
2854 1.9.2.2 tron
2855 1.9.2.2 tron s = splbio();
2856 1.9.2.2 tron
2857 1.9.2.2 tron if ((tr = twa_get_request(sc, TWA_CMD_AEN)) == NULL) {
2858 1.9.2.2 tron splx(s);
2859 1.9.2.2 tron return(EIO);
2860 1.9.2.2 tron }
2861 1.9.2.2 tron tr->tr_cmd_pkt_type |= TWA_CMD_PKT_TYPE_INTERNAL;
2862 1.9.2.2 tron tr->tr_callback = twa_aen_callback;
2863 1.9.2.2 tron tr->tr_data = malloc(TWA_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
2864 1.9.2.2 tron if (twa_request_sense(tr, 0) != 0) {
2865 1.9.2.2 tron if (tr->tr_data)
2866 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
2867 1.9.2.2 tron twa_release_request(tr);
2868 1.9.2.2 tron error = 1;
2869 1.9.2.2 tron }
2870 1.9.2.2 tron splx(s);
2871 1.9.2.2 tron
2872 1.9.2.2 tron return(error);
2873 1.9.2.2 tron }
2874 1.9.2.2 tron
2875 1.9.2.2 tron /*
2876 1.9.2.2 tron * Function name: twa_aen_callback
2877 1.9.2.2 tron * Description: Callback for requests to fetch AEN's.
2878 1.9.2.2 tron *
2879 1.9.2.2 tron * Input: tr -- ptr to completed request pkt
2880 1.9.2.2 tron * Output: None
2881 1.9.2.2 tron * Return value: None
2882 1.9.2.2 tron */
2883 1.9.2.2 tron static void
2884 1.9.2.2 tron twa_aen_callback(struct twa_request *tr)
2885 1.9.2.2 tron {
2886 1.9.2.2 tron int i;
2887 1.9.2.2 tron int fetch_more_aens = 0;
2888 1.9.2.2 tron struct twa_softc *sc = tr->tr_sc;
2889 1.9.2.2 tron struct twa_command_header *cmd_hdr =
2890 1.9.2.2 tron (struct twa_command_header *)(tr->tr_data);
2891 1.9.2.2 tron struct twa_command_9k *cmd =
2892 1.9.2.2 tron &(tr->tr_command->command.cmd_pkt_9k);
2893 1.9.2.2 tron
2894 1.9.2.2 tron if (! cmd->status) {
2895 1.9.2.2 tron if ((tr->tr_cmd_pkt_type & TWA_CMD_PKT_TYPE_9K) &&
2896 1.9.2.2 tron (cmd->cdb[0] == 0x3 /* REQUEST_SENSE */))
2897 1.9.2.2 tron if (twa_enqueue_aen(sc, cmd_hdr)
2898 1.9.2.2 tron != TWA_AEN_QUEUE_EMPTY)
2899 1.9.2.2 tron fetch_more_aens = 1;
2900 1.9.2.2 tron } else {
2901 1.9.2.2 tron cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2902 1.9.2.2 tron for (i = 0; i < 18; i++)
2903 1.9.2.2 tron printf("%x\t", tr->tr_command->cmd_hdr.sense_data[i]);
2904 1.9.2.2 tron
2905 1.9.2.2 tron printf(""); /* print new line */
2906 1.9.2.2 tron
2907 1.9.2.2 tron for (i = 0; i < 128; i++)
2908 1.9.2.2 tron printf("%x\t", ((int8_t *)(tr->tr_data))[i]);
2909 1.9.2.2 tron }
2910 1.9.2.2 tron if (tr->tr_data)
2911 1.9.2.2 tron free(tr->tr_data, M_DEVBUF);
2912 1.9.2.2 tron twa_release_request(tr);
2913 1.9.2.2 tron
2914 1.9.2.2 tron if (fetch_more_aens)
2915 1.9.2.2 tron twa_fetch_aen(sc);
2916 1.9.2.2 tron }
2917 1.9.2.2 tron
2918 1.9.2.2 tron /*
2919 1.9.2.2 tron * Function name: twa_enqueue_aen
2920 1.9.2.2 tron * Description: Queues AEN's to be supplied to user-space tools on request.
2921 1.9.2.2 tron *
2922 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2923 1.9.2.2 tron * cmd_hdr -- ptr to hdr of fw cmd pkt, from where the AEN
2924 1.9.2.2 tron * details can be retrieved.
2925 1.9.2.2 tron * Output: None
2926 1.9.2.2 tron * Return value: None
2927 1.9.2.2 tron */
2928 1.9.2.2 tron static uint16_t
2929 1.9.2.2 tron twa_enqueue_aen(struct twa_softc *sc, struct twa_command_header *cmd_hdr)
2930 1.9.2.2 tron {
2931 1.9.2.2 tron int rv, s;
2932 1.9.2.2 tron struct tw_cl_event_packet *event;
2933 1.9.2.2 tron uint16_t aen_code;
2934 1.9.2.2 tron unsigned long sync_time;
2935 1.9.2.2 tron
2936 1.9.2.2 tron s = splbio();
2937 1.9.2.2 tron aen_code = cmd_hdr->status_block.error;
2938 1.9.2.2 tron
2939 1.9.2.2 tron switch (aen_code) {
2940 1.9.2.2 tron case TWA_AEN_SYNC_TIME_WITH_HOST:
2941 1.9.2.2 tron
2942 1.9.2.2 tron sync_time = (time.tv_sec - (3 * 86400)) % 604800;
2943 1.9.2.2 tron rv = twa_set_param(sc, TWA_PARAM_TIME_TABLE,
2944 1.9.2.2 tron TWA_PARAM_TIME_SchedulerTime, 4,
2945 1.9.2.2 tron &sync_time, twa_aen_callback);
2946 1.9.2.2 tron #ifdef DIAGNOSTIC
2947 1.9.2.2 tron if (rv != 0)
2948 1.9.2.2 tron printf("%s: unable to sync time with ctlr\n",
2949 1.9.2.2 tron sc->twa_dv.dv_xname);
2950 1.9.2.2 tron #endif
2951 1.9.2.2 tron break;
2952 1.9.2.2 tron
2953 1.9.2.2 tron case TWA_AEN_QUEUE_EMPTY:
2954 1.9.2.2 tron break;
2955 1.9.2.2 tron
2956 1.9.2.2 tron default:
2957 1.9.2.2 tron /* Queue the event. */
2958 1.9.2.2 tron event = sc->twa_aen_queue[sc->twa_aen_head];
2959 1.9.2.2 tron if (event->retrieved == TWA_AEN_NOT_RETRIEVED)
2960 1.9.2.2 tron sc->twa_aen_queue_overflow = TRUE;
2961 1.9.2.2 tron event->severity =
2962 1.9.2.2 tron cmd_hdr->status_block.substatus_block.severity;
2963 1.9.2.2 tron event->time_stamp_sec = time.tv_sec;
2964 1.9.2.2 tron event->aen_code = aen_code;
2965 1.9.2.2 tron event->retrieved = TWA_AEN_NOT_RETRIEVED;
2966 1.9.2.2 tron event->sequence_id = ++(sc->twa_current_sequence_id);
2967 1.9.2.2 tron cmd_hdr->err_specific_desc[sizeof(cmd_hdr->err_specific_desc) - 1] = '\0';
2968 1.9.2.2 tron event->parameter_len = strlen(cmd_hdr->err_specific_desc);
2969 1.9.2.2 tron memcpy(event->parameter_data, cmd_hdr->err_specific_desc,
2970 1.9.2.2 tron event->parameter_len);
2971 1.9.2.2 tron
2972 1.9.2.2 tron if (event->severity < TWA_AEN_SEVERITY_DEBUG) {
2973 1.9.2.2 tron printf("%s: AEN 0x%04X: %s: %s: %s\n",
2974 1.9.2.2 tron sc->twa_dv.dv_xname,
2975 1.9.2.2 tron aen_code,
2976 1.9.2.2 tron twa_aen_severity_table[event->severity],
2977 1.9.2.2 tron twa_find_msg_string(twa_aen_table, aen_code),
2978 1.9.2.2 tron event->parameter_data);
2979 1.9.2.2 tron }
2980 1.9.2.2 tron
2981 1.9.2.2 tron if ((sc->twa_aen_head + 1) == TWA_Q_LENGTH)
2982 1.9.2.2 tron sc->twa_aen_queue_wrapped = TRUE;
2983 1.9.2.2 tron sc->twa_aen_head = (sc->twa_aen_head + 1) % TWA_Q_LENGTH;
2984 1.9.2.2 tron break;
2985 1.9.2.2 tron } /* switch */
2986 1.9.2.2 tron splx(s);
2987 1.9.2.2 tron
2988 1.9.2.2 tron return (aen_code);
2989 1.9.2.2 tron }
2990 1.9.2.2 tron
2991 1.9.2.2 tron /*
2992 1.9.2.2 tron * Function name: twa_find_aen
2993 1.9.2.2 tron * Description: Reports whether a given AEN ever occurred.
2994 1.9.2.2 tron *
2995 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
2996 1.9.2.2 tron * aen_code-- AEN to look for
2997 1.9.2.2 tron * Output: None
2998 1.9.2.2 tron * Return value: 0 -- success
2999 1.9.2.2 tron * non-zero-- failure
3000 1.9.2.2 tron */
3001 1.9.2.2 tron static int
3002 1.9.2.2 tron twa_find_aen(struct twa_softc *sc, uint16_t aen_code)
3003 1.9.2.2 tron {
3004 1.9.2.2 tron uint32_t last_index;
3005 1.9.2.2 tron int s;
3006 1.9.2.2 tron int i;
3007 1.9.2.2 tron
3008 1.9.2.2 tron s = splbio();
3009 1.9.2.2 tron
3010 1.9.2.2 tron if (sc->twa_aen_queue_wrapped)
3011 1.9.2.2 tron last_index = sc->twa_aen_head;
3012 1.9.2.2 tron else
3013 1.9.2.2 tron last_index = 0;
3014 1.9.2.2 tron
3015 1.9.2.2 tron i = sc->twa_aen_head;
3016 1.9.2.2 tron do {
3017 1.9.2.2 tron i = (i + TWA_Q_LENGTH - 1) % TWA_Q_LENGTH;
3018 1.9.2.2 tron if ((sc->twa_aen_queue[i])->aen_code == aen_code) {
3019 1.9.2.2 tron splx(s);
3020 1.9.2.2 tron return(0);
3021 1.9.2.2 tron }
3022 1.9.2.2 tron } while (i != last_index);
3023 1.9.2.2 tron
3024 1.9.2.2 tron splx(s);
3025 1.9.2.2 tron return(1);
3026 1.9.2.2 tron }
3027 1.9.2.2 tron
3028 1.9.2.2 tron static void inline
3029 1.9.2.2 tron twa_request_init(struct twa_request *tr, int flags)
3030 1.9.2.2 tron {
3031 1.9.2.2 tron tr->tr_data = NULL;
3032 1.9.2.2 tron tr->tr_real_data = NULL;
3033 1.9.2.2 tron tr->tr_length = 0;
3034 1.9.2.2 tron tr->tr_real_length = 0;
3035 1.9.2.2 tron tr->tr_status = TWA_CMD_SETUP;/* command is in setup phase */
3036 1.9.2.2 tron tr->tr_flags = flags;
3037 1.9.2.2 tron tr->tr_error = 0;
3038 1.9.2.2 tron tr->tr_callback = NULL;
3039 1.9.2.2 tron tr->tr_cmd_pkt_type = 0;
3040 1.9.2.2 tron tr->bp = 0;
3041 1.9.2.2 tron
3042 1.9.2.2 tron /*
3043 1.9.2.2 tron * Look at the status field in the command packet to see how
3044 1.9.2.2 tron * it completed the last time it was used, and zero out only
3045 1.9.2.2 tron * the portions that might have changed. Note that we don't
3046 1.9.2.2 tron * care to zero out the sglist.
3047 1.9.2.2 tron */
3048 1.9.2.2 tron if (tr->tr_command->command.cmd_pkt_9k.status)
3049 1.9.2.2 tron memset(tr->tr_command, 0,
3050 1.9.2.2 tron sizeof(struct twa_command_header) + 28);
3051 1.9.2.2 tron else
3052 1.9.2.2 tron memset(&(tr->tr_command->command), 0, 28);
3053 1.9.2.2 tron }
3054 1.9.2.2 tron
3055 1.9.2.2 tron struct twa_request *
3056 1.9.2.2 tron twa_get_request_wait(struct twa_softc *sc, int flags)
3057 1.9.2.2 tron {
3058 1.9.2.2 tron struct twa_request *tr;
3059 1.9.2.2 tron int s;
3060 1.9.2.2 tron
3061 1.9.2.2 tron KASSERT((flags & TWA_CMD_AEN) == 0);
3062 1.9.2.2 tron
3063 1.9.2.2 tron s = splbio();
3064 1.9.2.2 tron while ((tr = TAILQ_FIRST(&sc->twa_free)) == NULL) {
3065 1.9.2.2 tron sc->twa_sc_flags |= TWA_STATE_REQUEST_WAIT;
3066 1.9.2.2 tron (void) tsleep(&sc->twa_free, PRIBIO, "twaccb", hz);
3067 1.9.2.2 tron }
3068 1.9.2.2 tron TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3069 1.9.2.2 tron
3070 1.9.2.2 tron splx(s);
3071 1.9.2.2 tron
3072 1.9.2.2 tron twa_request_init(tr, flags);
3073 1.9.2.2 tron
3074 1.9.2.2 tron return(tr);
3075 1.9.2.2 tron }
3076 1.9.2.2 tron
3077 1.9.2.2 tron struct twa_request *
3078 1.9.2.2 tron twa_get_request(struct twa_softc *sc, int flags)
3079 1.9.2.2 tron {
3080 1.9.2.2 tron int s;
3081 1.9.2.2 tron struct twa_request *tr;
3082 1.9.2.2 tron
3083 1.9.2.2 tron /* Get a free request packet. */
3084 1.9.2.2 tron s = splbio();
3085 1.9.2.2 tron if (__predict_false((flags & TWA_CMD_AEN) != 0)) {
3086 1.9.2.2 tron
3087 1.9.2.2 tron if ((sc->sc_twa_request->tr_flags & TWA_CMD_AEN_BUSY) == 0) {
3088 1.9.2.2 tron tr = sc->sc_twa_request;
3089 1.9.2.2 tron flags |= TWA_CMD_AEN_BUSY;
3090 1.9.2.2 tron } else {
3091 1.9.2.2 tron splx(s);
3092 1.9.2.2 tron return (NULL);
3093 1.9.2.2 tron }
3094 1.9.2.2 tron } else {
3095 1.9.2.2 tron if (__predict_false((tr =
3096 1.9.2.2 tron TAILQ_FIRST(&sc->twa_free)) == NULL)) {
3097 1.9.2.2 tron splx(s);
3098 1.9.2.2 tron return (NULL);
3099 1.9.2.2 tron }
3100 1.9.2.2 tron TAILQ_REMOVE(&sc->twa_free, tr, tr_link);
3101 1.9.2.2 tron }
3102 1.9.2.2 tron splx(s);
3103 1.9.2.2 tron
3104 1.9.2.2 tron twa_request_init(tr, flags);
3105 1.9.2.2 tron
3106 1.9.2.2 tron return(tr);
3107 1.9.2.2 tron }
3108 1.9.2.2 tron
3109 1.9.2.2 tron /*
3110 1.9.2.2 tron * Print some information about the controller
3111 1.9.2.2 tron */
3112 1.9.2.2 tron static void
3113 1.9.2.2 tron twa_describe_controller(struct twa_softc *sc)
3114 1.9.2.2 tron {
3115 1.9.2.2 tron struct twa_param_9k *p[10];
3116 1.9.2.2 tron int i, rv = 0;
3117 1.9.2.2 tron uint32_t dsize;
3118 1.9.2.2 tron uint8_t ports;
3119 1.9.2.2 tron
3120 1.9.2.2 tron memset(p, sizeof(struct twa_param_9k *), 10);
3121 1.9.2.2 tron
3122 1.9.2.2 tron /* Get the port count. */
3123 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_CONTROLLER,
3124 1.9.2.2 tron TWA_PARAM_CONTROLLER_PortCount, 1, NULL, &p[0]);
3125 1.9.2.2 tron
3126 1.9.2.2 tron /* get version strings */
3127 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_FW,
3128 1.9.2.2 tron 16, NULL, &p[1]);
3129 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_BIOS,
3130 1.9.2.2 tron 16, NULL, &p[2]);
3131 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_Mon,
3132 1.9.2.2 tron 16, NULL, &p[3]);
3133 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCBA,
3134 1.9.2.2 tron 8, NULL, &p[4]);
3135 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_ATA,
3136 1.9.2.2 tron 8, NULL, &p[5]);
3137 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_VERSION, TWA_PARAM_VERSION_PCI,
3138 1.9.2.2 tron 8, NULL, &p[6]);
3139 1.9.2.2 tron rv |= twa_get_param(sc, TWA_PARAM_DRIVESUMMARY, TWA_PARAM_DRIVESTATUS,
3140 1.9.2.2 tron 16, NULL, &p[7]);
3141 1.9.2.2 tron
3142 1.9.2.2 tron if (rv) {
3143 1.9.2.2 tron /* some error occurred */
3144 1.9.2.2 tron aprint_error("%s: failed to fetch version information\n",
3145 1.9.2.2 tron sc->twa_dv.dv_xname);
3146 1.9.2.2 tron goto bail;
3147 1.9.2.2 tron }
3148 1.9.2.2 tron
3149 1.9.2.2 tron ports = *(uint8_t *)(p[0]->data);
3150 1.9.2.2 tron
3151 1.9.2.2 tron aprint_normal("%s: %d ports, Firmware %.16s, BIOS %.16s\n",
3152 1.9.2.2 tron sc->twa_dv.dv_xname, ports,
3153 1.9.2.2 tron p[1]->data, p[2]->data);
3154 1.9.2.2 tron
3155 1.9.2.2 tron aprint_verbose("%s: Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n",
3156 1.9.2.2 tron sc->twa_dv.dv_xname,
3157 1.9.2.2 tron p[3]->data, p[4]->data,
3158 1.9.2.2 tron p[5]->data, p[6]->data);
3159 1.9.2.2 tron
3160 1.9.2.2 tron for (i = 0; i < ports; i++) {
3161 1.9.2.2 tron
3162 1.9.2.2 tron if ((*((char *)(p[7]->data + i)) & TWA_DRIVE_DETECTED) == 0)
3163 1.9.2.2 tron continue;
3164 1.9.2.2 tron
3165 1.9.2.2 tron rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE,
3166 1.9.2.2 tron TWA_PARAM_DRIVEMODELINDEX,
3167 1.9.2.2 tron TWA_PARAM_DRIVEMODEL_LENGTH, NULL, &p[8]);
3168 1.9.2.2 tron
3169 1.9.2.2 tron if (rv != 0) {
3170 1.9.2.2 tron aprint_error("%s: unable to get drive model for port"
3171 1.9.2.2 tron " %d\n", sc->twa_dv.dv_xname, i);
3172 1.9.2.2 tron continue;
3173 1.9.2.2 tron }
3174 1.9.2.2 tron
3175 1.9.2.2 tron rv = twa_get_param(sc, TWA_PARAM_DRIVE_TABLE,
3176 1.9.2.2 tron TWA_PARAM_DRIVESIZEINDEX,
3177 1.9.2.2 tron TWA_PARAM_DRIVESIZE_LENGTH, NULL, &p[9]);
3178 1.9.2.2 tron
3179 1.9.2.2 tron if (rv != 0) {
3180 1.9.2.2 tron aprint_error("%s: unable to get drive size"
3181 1.9.2.2 tron " for port %d\n", sc->twa_dv.dv_xname,
3182 1.9.2.2 tron i);
3183 1.9.2.2 tron free(p[8], M_DEVBUF);
3184 1.9.2.2 tron continue;
3185 1.9.2.2 tron }
3186 1.9.2.2 tron
3187 1.9.2.2 tron dsize = *(uint32_t *)(p[9]->data);
3188 1.9.2.2 tron
3189 1.9.2.2 tron aprint_verbose("%s: port %d: %.40s %d MB\n",
3190 1.9.2.2 tron sc->twa_dv.dv_xname, i, p[8]->data, dsize / 2048);
3191 1.9.2.2 tron
3192 1.9.2.2 tron if (p[8])
3193 1.9.2.2 tron free(p[8], M_DEVBUF);
3194 1.9.2.2 tron if (p[9])
3195 1.9.2.2 tron free(p[9], M_DEVBUF);
3196 1.9.2.2 tron }
3197 1.9.2.2 tron bail:
3198 1.9.2.2 tron if (p[0])
3199 1.9.2.2 tron free(p[0], M_DEVBUF);
3200 1.9.2.2 tron if (p[1])
3201 1.9.2.2 tron free(p[1], M_DEVBUF);
3202 1.9.2.2 tron if (p[2])
3203 1.9.2.2 tron free(p[2], M_DEVBUF);
3204 1.9.2.2 tron if (p[3])
3205 1.9.2.2 tron free(p[3], M_DEVBUF);
3206 1.9.2.2 tron if (p[4])
3207 1.9.2.2 tron free(p[4], M_DEVBUF);
3208 1.9.2.2 tron if (p[5])
3209 1.9.2.2 tron free(p[5], M_DEVBUF);
3210 1.9.2.2 tron if (p[6])
3211 1.9.2.2 tron free(p[6], M_DEVBUF);
3212 1.9.2.2 tron }
3213 1.9.2.2 tron
3214 1.9.2.2 tron /*
3215 1.9.2.2 tron * Function name: twa_check_ctlr_state
3216 1.9.2.2 tron * Description: Makes sure that the fw status register reports a
3217 1.9.2.2 tron * proper status.
3218 1.9.2.2 tron *
3219 1.9.2.2 tron * Input: sc -- ptr to per ctlr structure
3220 1.9.2.2 tron * status_reg -- value in the status register
3221 1.9.2.2 tron * Output: None
3222 1.9.2.2 tron * Return value: 0 -- no errors
3223 1.9.2.2 tron * non-zero-- errors
3224 1.9.2.2 tron */
3225 1.9.2.2 tron static int
3226 1.9.2.2 tron twa_check_ctlr_state(struct twa_softc *sc, uint32_t status_reg)
3227 1.9.2.2 tron {
3228 1.9.2.2 tron int result = 0;
3229 1.9.2.2 tron struct timeval t1;
3230 1.9.2.2 tron static time_t last_warning[2] = {0, 0};
3231 1.9.2.2 tron
3232 1.9.2.2 tron /* Check if the 'micro-controller ready' bit is not set. */
3233 1.9.2.2 tron if ((status_reg & TWA_STATUS_EXPECTED_BITS) !=
3234 1.9.2.2 tron TWA_STATUS_EXPECTED_BITS) {
3235 1.9.2.2 tron
3236 1.9.2.2 tron microtime(&t1);
3237 1.9.2.2 tron
3238 1.9.2.2 tron last_warning[0] += (5 * 1000 * 100);
3239 1.9.2.2 tron
3240 1.9.2.2 tron if (t1.tv_usec > last_warning[0]) {
3241 1.9.2.2 tron microtime(&t1);
3242 1.9.2.2 tron last_warning[0] = t1.tv_usec;
3243 1.9.2.2 tron }
3244 1.9.2.2 tron result = 1;
3245 1.9.2.2 tron }
3246 1.9.2.2 tron
3247 1.9.2.2 tron /* Check if any error bits are set. */
3248 1.9.2.2 tron if ((status_reg & TWA_STATUS_UNEXPECTED_BITS) != 0) {
3249 1.9.2.2 tron
3250 1.9.2.2 tron microtime(&t1);
3251 1.9.2.2 tron last_warning[1] += (5 * 1000 * 100);
3252 1.9.2.2 tron if (t1.tv_usec > last_warning[1]) {
3253 1.9.2.2 tron microtime(&t1);
3254 1.9.2.2 tron last_warning[1] = t1.tv_usec;
3255 1.9.2.2 tron }
3256 1.9.2.2 tron if (status_reg & TWA_STATUS_PCI_PARITY_ERROR_INTERRUPT) {
3257 1.9.2.2 tron aprint_error("%s: clearing PCI parity error "
3258 1.9.2.2 tron "re-seat/move/replace card.\n",
3259 1.9.2.2 tron sc->twa_dv.dv_xname);
3260 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3261 1.9.2.2 tron TWA_CONTROL_CLEAR_PARITY_ERROR);
3262 1.9.2.2 tron pci_conf_write(sc->pc, sc->tag,
3263 1.9.2.2 tron PCI_COMMAND_STATUS_REG,
3264 1.9.2.2 tron TWA_PCI_CONFIG_CLEAR_PARITY_ERROR);
3265 1.9.2.2 tron result = 1;
3266 1.9.2.2 tron }
3267 1.9.2.2 tron if (status_reg & TWA_STATUS_PCI_ABORT_INTERRUPT) {
3268 1.9.2.2 tron aprint_error("%s: clearing PCI abort\n",
3269 1.9.2.2 tron sc->twa_dv.dv_xname);
3270 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3271 1.9.2.2 tron TWA_CONTROL_CLEAR_PCI_ABORT);
3272 1.9.2.2 tron pci_conf_write(sc->pc, sc->tag,
3273 1.9.2.2 tron PCI_COMMAND_STATUS_REG,
3274 1.9.2.2 tron TWA_PCI_CONFIG_CLEAR_PCI_ABORT);
3275 1.9.2.2 tron result = 1;
3276 1.9.2.2 tron }
3277 1.9.2.2 tron if (status_reg & TWA_STATUS_QUEUE_ERROR_INTERRUPT) {
3278 1.9.2.2 tron aprint_error("%s: clearing controller queue error\n",
3279 1.9.2.2 tron sc->twa_dv.dv_xname);
3280 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3281 1.9.2.2 tron TWA_CONTROL_CLEAR_PCI_ABORT);
3282 1.9.2.2 tron result = 1;
3283 1.9.2.2 tron }
3284 1.9.2.2 tron if (status_reg & TWA_STATUS_SBUF_WRITE_ERROR) {
3285 1.9.2.2 tron aprint_error("%s: clearing SBUF write error\n",
3286 1.9.2.2 tron sc->twa_dv.dv_xname);
3287 1.9.2.2 tron twa_outl(sc, TWA_CONTROL_REGISTER_OFFSET,
3288 1.9.2.2 tron TWA_CONTROL_CLEAR_SBUF_WRITE_ERROR);
3289 1.9.2.2 tron result = 1;
3290 1.9.2.2 tron }
3291 1.9.2.2 tron if (status_reg & TWA_STATUS_MICROCONTROLLER_ERROR) {
3292 1.9.2.2 tron aprint_error("%s: micro-controller error\n",
3293 1.9.2.2 tron sc->twa_dv.dv_xname);
3294 1.9.2.2 tron result = 1;
3295 1.9.2.2 tron }
3296 1.9.2.2 tron }
3297 1.9.2.2 tron return(result);
3298 1.9.2.2 tron }
3299