icp.c revision 1.8 1 1.8 thorpej /* $NetBSD: icp.c,v 1.8 2003/05/13 15:42:33 thorpej Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.8 thorpej * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.8 thorpej * by Andrew Doran, and by Jason R. Thorpe of Wasabi Systems, Inc.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad /*
40 1.1 ad * Copyright (c) 1999, 2000 Niklas Hallqvist. All rights reserved.
41 1.1 ad *
42 1.1 ad * Redistribution and use in source and binary forms, with or without
43 1.1 ad * modification, are permitted provided that the following conditions
44 1.1 ad * are met:
45 1.1 ad * 1. Redistributions of source code must retain the above copyright
46 1.1 ad * notice, this list of conditions and the following disclaimer.
47 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 ad * notice, this list of conditions and the following disclaimer in the
49 1.1 ad * documentation and/or other materials provided with the distribution.
50 1.1 ad * 3. All advertising materials mentioning features or use of this software
51 1.1 ad * must display the following acknowledgement:
52 1.1 ad * This product includes software developed by Niklas Hallqvist.
53 1.1 ad * 4. The name of the author may not be used to endorse or promote products
54 1.1 ad * derived from this software without specific prior written permission.
55 1.1 ad *
56 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57 1.1 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58 1.1 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59 1.1 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60 1.1 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 1.1 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62 1.1 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63 1.1 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 1.1 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65 1.1 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 1.1 ad *
67 1.1 ad * from OpenBSD: gdt_common.c,v 1.12 2001/07/04 06:43:18 niklas Exp
68 1.1 ad */
69 1.1 ad
70 1.1 ad /*
71 1.1 ad * This driver would not have written if it was not for the hardware donations
72 1.1 ad * from both ICP-Vortex and ko.neT. I want to thank them for their support.
73 1.1 ad *
74 1.1 ad * Re-worked for NetBSD by Andrew Doran. Test hardware kindly supplied by
75 1.1 ad * Intel.
76 1.8 thorpej *
77 1.8 thorpej * Support for the ICP-Vortex management tools added by
78 1.8 thorpej * Jason R. Thorpe of Wasabi Systems, Inc., based on code
79 1.8 thorpej * provided by Achim Leubner <achim.leubner (at) intel.com>
80 1.1 ad */
81 1.1 ad
82 1.1 ad #include <sys/cdefs.h>
83 1.8 thorpej __KERNEL_RCSID(0, "$NetBSD: icp.c,v 1.8 2003/05/13 15:42:33 thorpej Exp $");
84 1.1 ad
85 1.1 ad #include <sys/param.h>
86 1.1 ad #include <sys/systm.h>
87 1.1 ad #include <sys/kernel.h>
88 1.1 ad #include <sys/device.h>
89 1.1 ad #include <sys/queue.h>
90 1.1 ad #include <sys/proc.h>
91 1.1 ad #include <sys/buf.h>
92 1.1 ad #include <sys/endian.h>
93 1.1 ad #include <sys/malloc.h>
94 1.1 ad #include <sys/disk.h>
95 1.1 ad
96 1.1 ad #include <uvm/uvm_extern.h>
97 1.1 ad
98 1.1 ad #include <machine/bswap.h>
99 1.1 ad #include <machine/bus.h>
100 1.1 ad
101 1.1 ad #include <dev/pci/pcireg.h>
102 1.1 ad #include <dev/pci/pcivar.h>
103 1.1 ad #include <dev/pci/pcidevs.h>
104 1.1 ad
105 1.1 ad #include <dev/ic/icpreg.h>
106 1.1 ad #include <dev/ic/icpvar.h>
107 1.1 ad
108 1.8 thorpej #include <dev/scsipi/scsipi_all.h>
109 1.8 thorpej #include <dev/scsipi/scsiconf.h>
110 1.8 thorpej
111 1.1 ad int icp_async_event(struct icp_softc *, int);
112 1.1 ad void icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic);
113 1.1 ad void icp_chain(struct icp_softc *);
114 1.1 ad int icp_print(void *, const char *);
115 1.1 ad int icp_submatch(struct device *, struct cfdata *, void *);
116 1.1 ad void icp_watchdog(void *);
117 1.8 thorpej void icp_ucmd_intr(struct icp_ccb *);
118 1.8 thorpej
119 1.8 thorpej int icp_count; /* total # of controllers, for ioctl interface */
120 1.8 thorpej
121 1.8 thorpej /*
122 1.8 thorpej * Statistics for the ioctl interface to query.
123 1.8 thorpej *
124 1.8 thorpej * XXX Global. They should probably be made per-controller
125 1.8 thorpej * XXX at some point.
126 1.8 thorpej */
127 1.8 thorpej gdt_statist_t icp_stats;
128 1.1 ad
129 1.1 ad int
130 1.1 ad icp_init(struct icp_softc *icp, const char *intrstr)
131 1.1 ad {
132 1.1 ad struct icp_attach_args icpa;
133 1.1 ad struct icp_binfo binfo;
134 1.1 ad struct icp_ccb *ic;
135 1.1 ad u_int16_t cdev_cnt;
136 1.1 ad int i, j, state, feat, nsegs, rv, noscsi, nocache;
137 1.1 ad
138 1.5 simonb state = 0;
139 1.1 ad noscsi = 0;
140 1.1 ad nocache = 0;
141 1.1 ad
142 1.1 ad if (intrstr != NULL)
143 1.7 thorpej aprint_normal("%s: interrupting at %s\n", icp->icp_dv.dv_xname,
144 1.1 ad intrstr);
145 1.1 ad
146 1.1 ad SIMPLEQ_INIT(&icp->icp_ccb_queue);
147 1.1 ad SIMPLEQ_INIT(&icp->icp_ccb_freelist);
148 1.8 thorpej SIMPLEQ_INIT(&icp->icp_ucmd_queue);
149 1.1 ad callout_init(&icp->icp_wdog_callout);
150 1.1 ad
151 1.1 ad /*
152 1.1 ad * Allocate a scratch area.
153 1.1 ad */
154 1.1 ad if (bus_dmamap_create(icp->icp_dmat, ICP_SCRATCH_SIZE, 1,
155 1.1 ad ICP_SCRATCH_SIZE, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
156 1.1 ad &icp->icp_scr_dmamap) != 0) {
157 1.7 thorpej aprint_error("%s: cannot create scratch dmamap\n",
158 1.1 ad icp->icp_dv.dv_xname);
159 1.1 ad return (1);
160 1.1 ad }
161 1.1 ad state++;
162 1.1 ad
163 1.1 ad if (bus_dmamem_alloc(icp->icp_dmat, ICP_SCRATCH_SIZE, PAGE_SIZE, 0,
164 1.1 ad icp->icp_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
165 1.7 thorpej aprint_error("%s: cannot alloc scratch dmamem\n",
166 1.1 ad icp->icp_dv.dv_xname);
167 1.1 ad goto bail_out;
168 1.1 ad }
169 1.1 ad state++;
170 1.1 ad
171 1.1 ad if (bus_dmamem_map(icp->icp_dmat, icp->icp_scr_seg, nsegs,
172 1.1 ad ICP_SCRATCH_SIZE, &icp->icp_scr, 0)) {
173 1.7 thorpej aprint_error("%s: cannot map scratch dmamem\n",
174 1.7 thorpej icp->icp_dv.dv_xname);
175 1.1 ad goto bail_out;
176 1.1 ad }
177 1.1 ad state++;
178 1.1 ad
179 1.1 ad if (bus_dmamap_load(icp->icp_dmat, icp->icp_scr_dmamap, icp->icp_scr,
180 1.1 ad ICP_SCRATCH_SIZE, NULL, BUS_DMA_NOWAIT)) {
181 1.7 thorpej aprint_error("%s: cannot load scratch dmamap\n",
182 1.7 thorpej icp->icp_dv.dv_xname);
183 1.1 ad goto bail_out;
184 1.1 ad }
185 1.1 ad state++;
186 1.1 ad
187 1.1 ad /*
188 1.1 ad * Allocate and initialize the command control blocks.
189 1.1 ad */
190 1.1 ad ic = malloc(sizeof(*ic) * ICP_NCCBS, M_DEVBUF, M_NOWAIT | M_ZERO);
191 1.1 ad if ((icp->icp_ccbs = ic) == NULL) {
192 1.7 thorpej aprint_error("%s: malloc() failed\n", icp->icp_dv.dv_xname);
193 1.1 ad goto bail_out;
194 1.1 ad }
195 1.1 ad state++;
196 1.1 ad
197 1.1 ad for (i = 0; i < ICP_NCCBS; i++, ic++) {
198 1.1 ad /*
199 1.1 ad * The first two command indexes have special meanings, so
200 1.1 ad * we can't use them.
201 1.1 ad */
202 1.1 ad ic->ic_ident = i + 2;
203 1.1 ad rv = bus_dmamap_create(icp->icp_dmat, ICP_MAX_XFER,
204 1.1 ad ICP_MAXSG, ICP_MAX_XFER, 0,
205 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
206 1.1 ad &ic->ic_xfer_map);
207 1.1 ad if (rv != 0)
208 1.1 ad break;
209 1.1 ad icp->icp_nccbs++;
210 1.1 ad icp_ccb_free(icp, ic);
211 1.1 ad }
212 1.1 ad #ifdef DIAGNOSTIC
213 1.1 ad if (icp->icp_nccbs != ICP_NCCBS)
214 1.7 thorpej aprint_error("%s: %d/%d CCBs usable\n", icp->icp_dv.dv_xname,
215 1.1 ad icp->icp_nccbs, ICP_NCCBS);
216 1.1 ad #endif
217 1.1 ad
218 1.1 ad /*
219 1.1 ad * Initalize the controller.
220 1.1 ad */
221 1.1 ad if (!icp_cmd(icp, ICP_SCREENSERVICE, ICP_INIT, 0, 0, 0)) {
222 1.7 thorpej aprint_error("%s: screen service init error %d\n",
223 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
224 1.1 ad goto bail_out;
225 1.1 ad }
226 1.1 ad
227 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
228 1.7 thorpej aprint_error("%s: cache service init error %d\n",
229 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
230 1.1 ad goto bail_out;
231 1.1 ad }
232 1.1 ad
233 1.1 ad icp_cmd(icp, ICP_CACHESERVICE, ICP_UNFREEZE_IO, 0, 0, 0);
234 1.1 ad
235 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_MOUNT, 0xffff, 1, 0)) {
236 1.7 thorpej aprint_error("%s: cache service mount error %d\n",
237 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
238 1.1 ad goto bail_out;
239 1.1 ad }
240 1.1 ad
241 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
242 1.7 thorpej aprint_error("%s: cache service post-mount init error %d\n",
243 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
244 1.1 ad goto bail_out;
245 1.1 ad }
246 1.1 ad cdev_cnt = (u_int16_t)icp->icp_info;
247 1.8 thorpej icp->icp_fw_vers = icp->icp_service;
248 1.1 ad
249 1.1 ad if (!icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_INIT, 0, 0, 0)) {
250 1.7 thorpej aprint_error("%s: raw service init error %d\n",
251 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
252 1.1 ad goto bail_out;
253 1.1 ad }
254 1.1 ad
255 1.1 ad /*
256 1.1 ad * Set/get raw service features (scatter/gather).
257 1.1 ad */
258 1.1 ad feat = 0;
259 1.1 ad if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_SET_FEAT, ICP_SCATTER_GATHER,
260 1.1 ad 0, 0))
261 1.1 ad if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_GET_FEAT, 0, 0, 0))
262 1.1 ad feat = icp->icp_info;
263 1.1 ad
264 1.1 ad if ((feat & ICP_SCATTER_GATHER) == 0) {
265 1.1 ad #ifdef DIAGNOSTIC
266 1.7 thorpej aprint_normal(
267 1.7 thorpej "%s: scatter/gather not supported (raw service)\n",
268 1.1 ad icp->icp_dv.dv_xname);
269 1.1 ad #endif
270 1.1 ad noscsi = 1;
271 1.1 ad }
272 1.1 ad
273 1.1 ad /*
274 1.1 ad * Set/get cache service features (scatter/gather).
275 1.1 ad */
276 1.1 ad feat = 0;
277 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_SET_FEAT, 0,
278 1.1 ad ICP_SCATTER_GATHER, 0))
279 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_GET_FEAT, 0, 0, 0))
280 1.1 ad feat = icp->icp_info;
281 1.1 ad
282 1.1 ad if ((feat & ICP_SCATTER_GATHER) == 0) {
283 1.1 ad #ifdef DIAGNOSTIC
284 1.7 thorpej aprint_normal(
285 1.7 thorpej "%s: scatter/gather not supported (cache service)\n",
286 1.1 ad icp->icp_dv.dv_xname);
287 1.1 ad #endif
288 1.1 ad nocache = 1;
289 1.1 ad }
290 1.1 ad
291 1.1 ad /*
292 1.1 ad * Pull some information from the board and dump.
293 1.1 ad */
294 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_BOARD_INFO,
295 1.1 ad ICP_INVALID_CHANNEL, sizeof(struct icp_binfo))) {
296 1.7 thorpej aprint_error("%s: unable to retrive board info\n",
297 1.1 ad icp->icp_dv.dv_xname);
298 1.1 ad goto bail_out;
299 1.1 ad }
300 1.1 ad memcpy(&binfo, icp->icp_scr, sizeof(binfo));
301 1.1 ad
302 1.7 thorpej aprint_normal(
303 1.7 thorpej "%s: model <%s>, firmware <%s>, %d channel(s), %dMB memory\n",
304 1.1 ad icp->icp_dv.dv_xname, binfo.bi_type_string, binfo.bi_raid_string,
305 1.1 ad binfo.bi_chan_count, le32toh(binfo.bi_memsize) >> 20);
306 1.1 ad
307 1.1 ad /*
308 1.1 ad * Determine the number of devices, and number of openings per
309 1.1 ad * device.
310 1.1 ad */
311 1.1 ad if (!nocache) {
312 1.1 ad for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
313 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INFO, j, 0,
314 1.1 ad 0))
315 1.1 ad continue;
316 1.1 ad
317 1.1 ad icp->icp_cdr[j].cd_size = icp->icp_info;
318 1.1 ad icp->icp_ndevs++;
319 1.1 ad
320 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_DEVTYPE, j, 0,
321 1.1 ad 0))
322 1.1 ad icp->icp_cdr[j].cd_type = icp->icp_info;
323 1.1 ad }
324 1.1 ad }
325 1.1 ad
326 1.1 ad if (!noscsi)
327 1.1 ad icp->icp_ndevs += binfo.bi_chan_count;
328 1.1 ad
329 1.1 ad if (icp->icp_ndevs != 0)
330 1.1 ad icp->icp_openings =
331 1.1 ad (icp->icp_nccbs - ICP_NCCB_RESERVE) / icp->icp_ndevs;
332 1.1 ad #ifdef ICP_DEBUG
333 1.7 thorpej aprint_debug("%s: %d openings per device\n", icp->icp_dv.dv_xname,
334 1.1 ad icp->icp_openings);
335 1.1 ad #endif
336 1.1 ad
337 1.1 ad /*
338 1.1 ad * Attach SCSI channels.
339 1.1 ad */
340 1.1 ad if (!noscsi) {
341 1.1 ad struct icp_ioc_version *iv;
342 1.1 ad struct icp_rawioc *ri;
343 1.1 ad struct icp_getch *gc;
344 1.1 ad
345 1.1 ad iv = (struct icp_ioc_version *)icp->icp_scr;
346 1.1 ad iv->iv_version = htole32(ICP_IOC_NEWEST);
347 1.1 ad iv->iv_listents = ICP_MAXBUS;
348 1.1 ad iv->iv_firstchan = 0;
349 1.1 ad iv->iv_lastchan = ICP_MAXBUS - 1;
350 1.1 ad iv->iv_listoffset = htole32(sizeof(*iv));
351 1.1 ad
352 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
353 1.1 ad ICP_IOCHAN_RAW_DESC, ICP_INVALID_CHANNEL,
354 1.1 ad sizeof(*iv) + ICP_MAXBUS * sizeof(*ri))) {
355 1.1 ad ri = (struct icp_rawioc *)(iv + 1);
356 1.1 ad for (j = 0; j < binfo.bi_chan_count; j++, ri++)
357 1.1 ad icp->icp_bus_id[j] = ri->ri_procid;
358 1.1 ad } else {
359 1.1 ad /*
360 1.1 ad * Fall back to the old method.
361 1.1 ad */
362 1.1 ad gc = (struct icp_getch *)icp->icp_scr;
363 1.1 ad
364 1.1 ad for (i = 0; j < binfo.bi_chan_count; j++) {
365 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
366 1.1 ad ICP_SCSI_CHAN_CNT | ICP_L_CTRL_PATTERN,
367 1.1 ad ICP_IO_CHANNEL | ICP_INVALID_CHANNEL,
368 1.1 ad sizeof(*gc))) {
369 1.7 thorpej aprint_error(
370 1.7 thorpej "%s: unable to get chan info",
371 1.1 ad icp->icp_dv.dv_xname);
372 1.1 ad goto bail_out;
373 1.1 ad }
374 1.1 ad icp->icp_bus_id[j] = gc->gc_scsiid;
375 1.1 ad }
376 1.1 ad }
377 1.1 ad
378 1.1 ad for (j = 0; j < binfo.bi_chan_count; j++) {
379 1.1 ad if (icp->icp_bus_id[j] > ICP_MAXID_FC)
380 1.1 ad icp->icp_bus_id[j] = ICP_MAXID_FC;
381 1.1 ad
382 1.1 ad icpa.icpa_unit = j + ICPA_UNIT_SCSI;
383 1.1 ad config_found_sm(&icp->icp_dv, &icpa, icp_print,
384 1.1 ad icp_submatch);
385 1.1 ad }
386 1.1 ad }
387 1.1 ad
388 1.1 ad /*
389 1.1 ad * Attach cache devices.
390 1.1 ad */
391 1.1 ad if (!nocache) {
392 1.1 ad for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
393 1.1 ad if (icp->icp_cdr[j].cd_size == 0)
394 1.1 ad continue;
395 1.1 ad
396 1.1 ad icpa.icpa_unit = j;
397 1.1 ad config_found_sm(&icp->icp_dv, &icpa, icp_print,
398 1.1 ad icp_submatch);
399 1.1 ad }
400 1.1 ad }
401 1.1 ad
402 1.1 ad /*
403 1.1 ad * Start the watchdog.
404 1.1 ad */
405 1.1 ad icp_watchdog(icp);
406 1.8 thorpej
407 1.8 thorpej /*
408 1.8 thorpej * Count the controller, and we're done!
409 1.8 thorpej */
410 1.8 thorpej icp_count++;
411 1.8 thorpej
412 1.1 ad return (0);
413 1.1 ad
414 1.1 ad bail_out:
415 1.1 ad if (state > 4)
416 1.1 ad for (j = 0; j < i; j++)
417 1.1 ad bus_dmamap_destroy(icp->icp_dmat,
418 1.1 ad icp->icp_ccbs[j].ic_xfer_map);
419 1.1 ad if (state > 3)
420 1.1 ad free(icp->icp_ccbs, M_DEVBUF);
421 1.1 ad if (state > 2)
422 1.1 ad bus_dmamap_unload(icp->icp_dmat, icp->icp_scr_dmamap);
423 1.1 ad if (state > 1)
424 1.1 ad bus_dmamem_unmap(icp->icp_dmat, icp->icp_scr,
425 1.1 ad ICP_SCRATCH_SIZE);
426 1.1 ad if (state > 0)
427 1.1 ad bus_dmamem_free(icp->icp_dmat, icp->icp_scr_seg, nsegs);
428 1.1 ad bus_dmamap_destroy(icp->icp_dmat, icp->icp_scr_dmamap);
429 1.1 ad
430 1.1 ad return (1);
431 1.1 ad }
432 1.1 ad
433 1.1 ad void
434 1.1 ad icp_watchdog(void *cookie)
435 1.1 ad {
436 1.1 ad struct icp_softc *icp;
437 1.1 ad int s;
438 1.1 ad
439 1.1 ad icp = cookie;
440 1.1 ad
441 1.1 ad s = splbio();
442 1.1 ad icp_intr(icp);
443 1.8 thorpej if (ICP_HAS_WORK(icp))
444 1.1 ad icp_ccb_enqueue(icp, NULL);
445 1.1 ad splx(s);
446 1.1 ad
447 1.1 ad callout_reset(&icp->icp_wdog_callout, hz * ICP_WATCHDOG_FREQ,
448 1.1 ad icp_watchdog, icp);
449 1.1 ad }
450 1.1 ad
451 1.1 ad int
452 1.1 ad icp_print(void *aux, const char *pnp)
453 1.1 ad {
454 1.1 ad struct icp_attach_args *icpa;
455 1.1 ad const char *str;
456 1.1 ad
457 1.1 ad icpa = (struct icp_attach_args *)aux;
458 1.1 ad
459 1.1 ad if (pnp != NULL) {
460 1.1 ad if (icpa->icpa_unit < ICPA_UNIT_SCSI)
461 1.1 ad str = "block device";
462 1.1 ad else
463 1.1 ad str = "SCSI channel";
464 1.6 thorpej aprint_normal("%s at %s", str, pnp);
465 1.1 ad }
466 1.6 thorpej aprint_normal(" unit %d", icpa->icpa_unit);
467 1.1 ad
468 1.1 ad return (UNCONF);
469 1.1 ad }
470 1.1 ad
471 1.1 ad int
472 1.1 ad icp_submatch(struct device *parent, struct cfdata *cf, void *aux)
473 1.1 ad {
474 1.1 ad struct icp_attach_args *icpa;
475 1.1 ad
476 1.1 ad icpa = (struct icp_attach_args *)aux;
477 1.1 ad
478 1.1 ad if (cf->icpacf_unit != ICPCF_UNIT_DEFAULT &&
479 1.1 ad cf->icpacf_unit != icpa->icpa_unit)
480 1.1 ad return (0);
481 1.1 ad
482 1.3 thorpej return (config_match(parent, cf, aux));
483 1.1 ad }
484 1.1 ad
485 1.1 ad int
486 1.8 thorpej icp_async_event(struct icp_softc *icp, int service)
487 1.1 ad {
488 1.1 ad
489 1.8 thorpej if (service == ICP_SCREENSERVICE) {
490 1.8 thorpej if (icp->icp_status == ICP_S_MSG_REQUEST) {
491 1.8 thorpej /* XXX */
492 1.8 thorpej }
493 1.8 thorpej } else {
494 1.8 thorpej if ((icp->icp_fw_vers & 0xff) >= 0x1a) {
495 1.8 thorpej icp->icp_evt.size = 0;
496 1.8 thorpej icp->icp_evt.eu.async.ionode = icp->icp_dv.dv_unit;
497 1.8 thorpej icp->icp_evt.eu.async.status = icp->icp_status;
498 1.8 thorpej /*
499 1.8 thorpej * Severity and event string are filled in by the
500 1.8 thorpej * hardware interface interrupt handler.
501 1.8 thorpej */
502 1.8 thorpej printf("%s: %s\n", icp->icp_dv.dv_xname,
503 1.8 thorpej icp->icp_evt.event_string);
504 1.8 thorpej } else {
505 1.8 thorpej icp->icp_evt.size = sizeof(icp->icp_evt.eu.async);
506 1.8 thorpej icp->icp_evt.eu.async.ionode = icp->icp_dv.dv_unit;
507 1.8 thorpej icp->icp_evt.eu.async.service = service;
508 1.8 thorpej icp->icp_evt.eu.async.status = icp->icp_status;
509 1.8 thorpej icp->icp_evt.eu.async.info = icp->icp_info;
510 1.8 thorpej /* XXXJRT FIX THIS */
511 1.8 thorpej *(u_int32_t *) icp->icp_evt.eu.async.scsi_coord =
512 1.8 thorpej icp->icp_info2;
513 1.8 thorpej }
514 1.8 thorpej icp_store_event(icp, GDT_ES_ASYNC, service, &icp->icp_evt);
515 1.8 thorpej }
516 1.8 thorpej
517 1.8 thorpej return (0);
518 1.1 ad }
519 1.1 ad
520 1.1 ad int
521 1.1 ad icp_intr(void *cookie)
522 1.1 ad {
523 1.1 ad struct icp_softc *icp;
524 1.1 ad struct icp_intr_ctx ctx;
525 1.1 ad struct icp_ccb *ic;
526 1.1 ad
527 1.1 ad icp = cookie;
528 1.1 ad
529 1.1 ad ctx.istatus = (*icp->icp_get_status)(icp);
530 1.1 ad if (!ctx.istatus) {
531 1.1 ad icp->icp_status = ICP_S_NO_STATUS;
532 1.1 ad return (0);
533 1.1 ad }
534 1.1 ad
535 1.1 ad (*icp->icp_intr)(icp, &ctx);
536 1.1 ad
537 1.1 ad icp->icp_status = ctx.cmd_status;
538 1.8 thorpej icp->icp_service = ctx.service;
539 1.1 ad icp->icp_info = ctx.info;
540 1.1 ad icp->icp_info2 = ctx.info2;
541 1.1 ad
542 1.1 ad switch (ctx.istatus) {
543 1.1 ad case ICP_ASYNCINDEX:
544 1.1 ad icp_async_event(icp, ctx.service);
545 1.1 ad return (1);
546 1.1 ad
547 1.1 ad case ICP_SPEZINDEX:
548 1.1 ad printf("%s: uninitialized or unknown service (%d/%d)\n",
549 1.1 ad icp->icp_dv.dv_xname, ctx.info, ctx.info2);
550 1.8 thorpej icp->icp_evt.size = sizeof(icp->icp_evt.eu.driver);
551 1.8 thorpej icp->icp_evt.eu.driver.ionode = icp->icp_dv.dv_unit;
552 1.8 thorpej icp_store_event(icp, GDT_ES_DRIVER, 4, &icp->icp_evt);
553 1.1 ad return (1);
554 1.1 ad }
555 1.1 ad
556 1.1 ad if ((ctx.istatus - 2) > icp->icp_nccbs)
557 1.1 ad panic("icp_intr: bad command index returned");
558 1.1 ad
559 1.1 ad ic = &icp->icp_ccbs[ctx.istatus - 2];
560 1.1 ad ic->ic_status = icp->icp_status;
561 1.1 ad
562 1.8 thorpej if ((ic->ic_flags & IC_ALLOCED) == 0) {
563 1.8 thorpej /* XXX ICP's "iir" driver just sends an event here. */
564 1.1 ad panic("icp_intr: inactive CCB identified");
565 1.8 thorpej }
566 1.1 ad
567 1.1 ad switch (icp->icp_status) {
568 1.1 ad case ICP_S_BSY:
569 1.1 ad #ifdef ICP_DEBUG
570 1.1 ad printf("%s: ICP_S_BSY received\n", icp->icp_dv.dv_xname);
571 1.1 ad #endif
572 1.8 thorpej if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
573 1.8 thorpej SIMPLEQ_INSERT_HEAD(&icp->icp_ucmd_queue, ic, ic_chain);
574 1.8 thorpej else
575 1.8 thorpej SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_queue, ic, ic_chain);
576 1.1 ad break;
577 1.1 ad
578 1.1 ad default:
579 1.1 ad ic->ic_flags |= IC_COMPLETE;
580 1.1 ad
581 1.1 ad if ((ic->ic_flags & IC_WAITING) != 0)
582 1.1 ad wakeup(ic);
583 1.1 ad else if (ic->ic_intr != NULL)
584 1.1 ad (*ic->ic_intr)(ic);
585 1.1 ad
586 1.8 thorpej if (ICP_HAS_WORK(icp))
587 1.1 ad icp_ccb_enqueue(icp, NULL);
588 1.1 ad
589 1.1 ad break;
590 1.1 ad }
591 1.1 ad
592 1.1 ad return (1);
593 1.1 ad }
594 1.1 ad
595 1.8 thorpej struct icp_ucmd_ctx {
596 1.8 thorpej gdt_ucmd_t *iu_ucmd;
597 1.8 thorpej u_int32_t iu_cnt;
598 1.8 thorpej };
599 1.8 thorpej
600 1.8 thorpej void
601 1.8 thorpej icp_ucmd_intr(struct icp_ccb *ic)
602 1.8 thorpej {
603 1.8 thorpej struct icp_softc *icp = (void *) ic->ic_dv;
604 1.8 thorpej struct icp_ucmd_ctx *iu = ic->ic_context;
605 1.8 thorpej gdt_ucmd_t *ucmd = iu->iu_ucmd;
606 1.8 thorpej
607 1.8 thorpej ucmd->status = icp->icp_status;
608 1.8 thorpej ucmd->info = icp->icp_info;
609 1.8 thorpej
610 1.8 thorpej if (iu->iu_cnt != 0) {
611 1.8 thorpej bus_dmamap_sync(icp->icp_dmat,
612 1.8 thorpej icp->icp_scr_dmamap,
613 1.8 thorpej ICP_SCRATCH_UCMD, iu->iu_cnt,
614 1.8 thorpej BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
615 1.8 thorpej memcpy(ucmd->data,
616 1.8 thorpej icp->icp_scr + ICP_SCRATCH_UCMD, iu->iu_cnt);
617 1.8 thorpej }
618 1.8 thorpej
619 1.8 thorpej icp->icp_ucmd_ccb = NULL;
620 1.8 thorpej
621 1.8 thorpej ic->ic_flags |= IC_COMPLETE;
622 1.8 thorpej wakeup(ic);
623 1.8 thorpej }
624 1.8 thorpej
625 1.8 thorpej /*
626 1.8 thorpej * NOTE: We assume that it is safe to sleep here!
627 1.8 thorpej */
628 1.1 ad int
629 1.1 ad icp_cmd(struct icp_softc *icp, u_int8_t service, u_int16_t opcode,
630 1.1 ad u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
631 1.1 ad {
632 1.1 ad struct icp_ioctlcmd *icmd;
633 1.1 ad struct icp_cachecmd *cc;
634 1.1 ad struct icp_rawcmd *rc;
635 1.1 ad int retries, rv;
636 1.1 ad struct icp_ccb *ic;
637 1.1 ad
638 1.1 ad retries = ICP_RETRIES;
639 1.1 ad
640 1.1 ad do {
641 1.8 thorpej ic = icp_ccb_alloc_wait(icp);
642 1.1 ad memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
643 1.1 ad ic->ic_cmd.cmd_opcode = htole16(opcode);
644 1.1 ad
645 1.1 ad switch (service) {
646 1.1 ad case ICP_CACHESERVICE:
647 1.1 ad if (opcode == ICP_IOCTL) {
648 1.1 ad icmd = &ic->ic_cmd.cmd_packet.ic;
649 1.1 ad icmd->ic_subfunc = htole16(arg1);
650 1.1 ad icmd->ic_channel = htole32(arg2);
651 1.1 ad icmd->ic_bufsize = htole32(arg3);
652 1.1 ad icmd->ic_addr =
653 1.1 ad htole32(icp->icp_scr_seg[0].ds_addr);
654 1.1 ad
655 1.1 ad bus_dmamap_sync(icp->icp_dmat,
656 1.1 ad icp->icp_scr_dmamap, 0, arg3,
657 1.1 ad BUS_DMASYNC_PREWRITE |
658 1.1 ad BUS_DMASYNC_PREREAD);
659 1.1 ad } else {
660 1.1 ad cc = &ic->ic_cmd.cmd_packet.cc;
661 1.1 ad cc->cc_deviceno = htole16(arg1);
662 1.1 ad cc->cc_blockno = htole32(arg2);
663 1.1 ad }
664 1.1 ad break;
665 1.1 ad
666 1.1 ad case ICP_SCSIRAWSERVICE:
667 1.1 ad rc = &ic->ic_cmd.cmd_packet.rc;
668 1.1 ad rc->rc_direction = htole32(arg1);
669 1.1 ad rc->rc_bus = arg2;
670 1.1 ad rc->rc_target = arg3;
671 1.1 ad rc->rc_lun = arg3 >> 8;
672 1.1 ad break;
673 1.1 ad }
674 1.1 ad
675 1.1 ad ic->ic_service = service;
676 1.1 ad ic->ic_cmdlen = sizeof(ic->ic_cmd);
677 1.1 ad rv = icp_ccb_poll(icp, ic, 10000);
678 1.1 ad
679 1.1 ad switch (service) {
680 1.1 ad case ICP_CACHESERVICE:
681 1.1 ad if (opcode == ICP_IOCTL) {
682 1.1 ad bus_dmamap_sync(icp->icp_dmat,
683 1.1 ad icp->icp_scr_dmamap, 0, arg3,
684 1.1 ad BUS_DMASYNC_POSTWRITE |
685 1.1 ad BUS_DMASYNC_POSTREAD);
686 1.1 ad }
687 1.1 ad break;
688 1.1 ad }
689 1.1 ad
690 1.1 ad icp_ccb_free(icp, ic);
691 1.1 ad } while (rv != 0 && --retries > 0);
692 1.1 ad
693 1.1 ad return (icp->icp_status == ICP_S_OK);
694 1.1 ad }
695 1.1 ad
696 1.8 thorpej int
697 1.8 thorpej icp_ucmd(struct icp_softc *icp, gdt_ucmd_t *ucmd)
698 1.8 thorpej {
699 1.8 thorpej struct icp_ccb *ic;
700 1.8 thorpej struct icp_ucmd_ctx iu;
701 1.8 thorpej u_int32_t cnt;
702 1.8 thorpej int error;
703 1.8 thorpej
704 1.8 thorpej if (ucmd->service == ICP_CACHESERVICE) {
705 1.8 thorpej if (ucmd->command.cmd_opcode == ICP_IOCTL) {
706 1.8 thorpej cnt = ucmd->command.cmd_packet.ic.ic_bufsize;
707 1.8 thorpej if (cnt > GDT_SCRATCH_SZ) {
708 1.8 thorpej printf("%s: scratch buffer too small (%d/%d)\n",
709 1.8 thorpej icp->icp_dv.dv_xname, GDT_SCRATCH_SZ, cnt);
710 1.8 thorpej return (EINVAL);
711 1.8 thorpej }
712 1.8 thorpej } else {
713 1.8 thorpej cnt = ucmd->command.cmd_packet.cc.cc_blockcnt *
714 1.8 thorpej ICP_SECTOR_SIZE;
715 1.8 thorpej if (cnt > GDT_SCRATCH_SZ) {
716 1.8 thorpej printf("%s: scratch buffer too small (%d/%d)\n",
717 1.8 thorpej icp->icp_dv.dv_xname, GDT_SCRATCH_SZ, cnt);
718 1.8 thorpej return (EINVAL);
719 1.8 thorpej }
720 1.8 thorpej }
721 1.8 thorpej } else {
722 1.8 thorpej cnt = ucmd->command.cmd_packet.rc.rc_sdlen +
723 1.8 thorpej ucmd->command.cmd_packet.rc.rc_sense_len;
724 1.8 thorpej if (cnt > GDT_SCRATCH_SZ) {
725 1.8 thorpej printf("%s: scratch buffer too small (%d/%d)\n",
726 1.8 thorpej icp->icp_dv.dv_xname, GDT_SCRATCH_SZ, cnt);
727 1.8 thorpej return (EINVAL);
728 1.8 thorpej }
729 1.8 thorpej }
730 1.8 thorpej
731 1.8 thorpej iu.iu_ucmd = ucmd;
732 1.8 thorpej iu.iu_cnt = cnt;
733 1.8 thorpej
734 1.8 thorpej ic = icp_ccb_alloc_wait(icp);
735 1.8 thorpej memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
736 1.8 thorpej ic->ic_cmd.cmd_opcode = htole16(ucmd->command.cmd_opcode);
737 1.8 thorpej
738 1.8 thorpej if (ucmd->service == ICP_CACHESERVICE) {
739 1.8 thorpej if (ucmd->command.cmd_opcode == ICP_IOCTL) {
740 1.8 thorpej struct icp_ioctlcmd *icmd, *uicmd;
741 1.8 thorpej
742 1.8 thorpej icmd = &ic->ic_cmd.cmd_packet.ic;
743 1.8 thorpej uicmd = &ucmd->command.cmd_packet.ic;
744 1.8 thorpej
745 1.8 thorpej icmd->ic_subfunc = htole16(uicmd->ic_subfunc);
746 1.8 thorpej icmd->ic_channel = htole32(uicmd->ic_channel);
747 1.8 thorpej icmd->ic_bufsize = htole32(uicmd->ic_bufsize);
748 1.8 thorpej icmd->ic_addr =
749 1.8 thorpej htole32(icp->icp_scr_seg[0].ds_addr +
750 1.8 thorpej ICP_SCRATCH_UCMD);
751 1.8 thorpej } else {
752 1.8 thorpej struct icp_cachecmd *cc, *ucc;
753 1.8 thorpej
754 1.8 thorpej cc = &ic->ic_cmd.cmd_packet.cc;
755 1.8 thorpej ucc = &ucmd->command.cmd_packet.cc;
756 1.8 thorpej
757 1.8 thorpej cc->cc_deviceno = htole16(ucc->cc_deviceno);
758 1.8 thorpej cc->cc_blockno = htole32(ucc->cc_blockno);
759 1.8 thorpej cc->cc_blockcnt = htole32(ucc->cc_blockcnt);
760 1.8 thorpej cc->cc_addr = htole32(0xffffffffU);
761 1.8 thorpej cc->cc_nsgent = htole32(1);
762 1.8 thorpej cc->cc_sg[0].sg_addr =
763 1.8 thorpej htole32(icp->icp_scr_seg[0].ds_addr +
764 1.8 thorpej ICP_SCRATCH_UCMD);
765 1.8 thorpej cc->cc_sg[0].sg_len = htole32(cnt);
766 1.8 thorpej }
767 1.8 thorpej } else {
768 1.8 thorpej struct icp_rawcmd *rc, *urc;
769 1.8 thorpej
770 1.8 thorpej rc = &ic->ic_cmd.cmd_packet.rc;
771 1.8 thorpej urc = &ucmd->command.cmd_packet.rc;
772 1.8 thorpej
773 1.8 thorpej rc->rc_direction = htole32(urc->rc_direction);
774 1.8 thorpej rc->rc_sdata = htole32(0xffffffffU);
775 1.8 thorpej rc->rc_sdlen = htole32(urc->rc_sdlen);
776 1.8 thorpej rc->rc_clen = htole32(urc->rc_clen);
777 1.8 thorpej memcpy(rc->rc_cdb, urc->rc_cdb, sizeof(rc->rc_cdb));
778 1.8 thorpej rc->rc_target = urc->rc_target;
779 1.8 thorpej rc->rc_lun = urc->rc_lun;
780 1.8 thorpej rc->rc_bus = urc->rc_bus;
781 1.8 thorpej rc->rc_sense_len = htole32(urc->rc_sense_len);
782 1.8 thorpej rc->rc_sense_addr =
783 1.8 thorpej htole32(icp->icp_scr_seg[0].ds_addr +
784 1.8 thorpej ICP_SCRATCH_UCMD + urc->rc_sdlen);
785 1.8 thorpej rc->rc_nsgent = htole32(1);
786 1.8 thorpej rc->rc_sg[0].sg_addr =
787 1.8 thorpej htole32(icp->icp_scr_seg[0].ds_addr + ICP_SCRATCH_UCMD);
788 1.8 thorpej rc->rc_sg[0].sg_len = htole32(cnt - urc->rc_sense_len);
789 1.8 thorpej }
790 1.8 thorpej
791 1.8 thorpej ic->ic_service = ucmd->service;
792 1.8 thorpej ic->ic_cmdlen = sizeof(ic->ic_cmd);
793 1.8 thorpej ic->ic_context = &iu;
794 1.8 thorpej
795 1.8 thorpej /*
796 1.8 thorpej * XXX What units are ucmd->timeout in? Until we know, we
797 1.8 thorpej * XXX just pull a number out of thin air.
798 1.8 thorpej */
799 1.8 thorpej if (__predict_false((error = icp_ccb_wait_user(icp, ic, 30000)) != 0))
800 1.8 thorpej printf("%s: error %d waiting for ucmd to complete\n",
801 1.8 thorpej icp->icp_dv.dv_xname, error);
802 1.8 thorpej
803 1.8 thorpej /* icp_ucmd_intr() has updated ucmd. */
804 1.8 thorpej icp_ccb_free(icp, ic);
805 1.8 thorpej
806 1.8 thorpej return (error);
807 1.8 thorpej }
808 1.8 thorpej
809 1.1 ad struct icp_ccb *
810 1.1 ad icp_ccb_alloc(struct icp_softc *icp)
811 1.1 ad {
812 1.1 ad struct icp_ccb *ic;
813 1.1 ad int s;
814 1.1 ad
815 1.1 ad s = splbio();
816 1.8 thorpej if (__predict_false((ic =
817 1.8 thorpej SIMPLEQ_FIRST(&icp->icp_ccb_freelist)) == NULL)) {
818 1.8 thorpej splx(s);
819 1.8 thorpej return (NULL);
820 1.8 thorpej }
821 1.8 thorpej SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
822 1.8 thorpej splx(s);
823 1.8 thorpej
824 1.8 thorpej ic->ic_flags = IC_ALLOCED;
825 1.8 thorpej return (ic);
826 1.8 thorpej }
827 1.8 thorpej
828 1.8 thorpej struct icp_ccb *
829 1.8 thorpej icp_ccb_alloc_wait(struct icp_softc *icp)
830 1.8 thorpej {
831 1.8 thorpej struct icp_ccb *ic;
832 1.8 thorpej int s;
833 1.8 thorpej
834 1.8 thorpej s = splbio();
835 1.8 thorpej while ((ic = SIMPLEQ_FIRST(&icp->icp_ccb_freelist)) == NULL) {
836 1.8 thorpej icp->icp_flags |= ICP_F_WAIT_CCB;
837 1.8 thorpej (void) tsleep(&icp->icp_ccb_freelist, PRIBIO, "icpccb", 0);
838 1.8 thorpej }
839 1.2 lukem SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
840 1.1 ad splx(s);
841 1.1 ad
842 1.1 ad ic->ic_flags = IC_ALLOCED;
843 1.1 ad return (ic);
844 1.1 ad }
845 1.1 ad
846 1.1 ad void
847 1.1 ad icp_ccb_free(struct icp_softc *icp, struct icp_ccb *ic)
848 1.1 ad {
849 1.1 ad int s;
850 1.1 ad
851 1.1 ad s = splbio();
852 1.1 ad ic->ic_flags = 0;
853 1.1 ad ic->ic_intr = NULL;
854 1.1 ad SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_freelist, ic, ic_chain);
855 1.8 thorpej if (__predict_false((icp->icp_flags & ICP_F_WAIT_CCB) != 0)) {
856 1.8 thorpej icp->icp_flags &= ~ICP_F_WAIT_CCB;
857 1.8 thorpej wakeup(&icp->icp_ccb_freelist);
858 1.8 thorpej }
859 1.1 ad splx(s);
860 1.1 ad }
861 1.1 ad
862 1.1 ad void
863 1.1 ad icp_ccb_enqueue(struct icp_softc *icp, struct icp_ccb *ic)
864 1.1 ad {
865 1.1 ad int s;
866 1.1 ad
867 1.1 ad s = splbio();
868 1.1 ad
869 1.8 thorpej if (ic != NULL) {
870 1.8 thorpej if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
871 1.8 thorpej SIMPLEQ_INSERT_TAIL(&icp->icp_ucmd_queue, ic, ic_chain);
872 1.8 thorpej else
873 1.8 thorpej SIMPLEQ_INSERT_TAIL(&icp->icp_ccb_queue, ic, ic_chain);
874 1.8 thorpej }
875 1.8 thorpej
876 1.8 thorpej for (;;) {
877 1.8 thorpej if (__predict_false((ic =
878 1.8 thorpej SIMPLEQ_FIRST(&icp->icp_ucmd_queue)) != NULL)) {
879 1.8 thorpej struct icp_ucmd_ctx *iu = ic->ic_context;
880 1.8 thorpej gdt_ucmd_t *ucmd = iu->iu_ucmd;
881 1.1 ad
882 1.8 thorpej /*
883 1.8 thorpej * All user-generated commands share the same
884 1.8 thorpej * scratch space, so if one is already running,
885 1.8 thorpej * we have to stall the command queue.
886 1.8 thorpej */
887 1.8 thorpej if (icp->icp_ucmd_ccb != NULL)
888 1.8 thorpej break;
889 1.8 thorpej icp->icp_ucmd_ccb = ic;
890 1.8 thorpej
891 1.8 thorpej if (iu->iu_cnt != 0) {
892 1.8 thorpej memcpy(icp->icp_scr + ICP_SCRATCH_UCMD,
893 1.8 thorpej ucmd->data, iu->iu_cnt);
894 1.8 thorpej bus_dmamap_sync(icp->icp_dmat,
895 1.8 thorpej icp->icp_scr_dmamap,
896 1.8 thorpej ICP_SCRATCH_UCMD, iu->iu_cnt,
897 1.8 thorpej BUS_DMASYNC_PREREAD |
898 1.8 thorpej BUS_DMASYNC_PREWRITE);
899 1.8 thorpej }
900 1.8 thorpej } else if ((ic = SIMPLEQ_FIRST(&icp->icp_ccb_queue)) == NULL)
901 1.8 thorpej break;
902 1.1 ad if ((*icp->icp_test_busy)(icp))
903 1.1 ad break;
904 1.1 ad icp_ccb_submit(icp, ic);
905 1.8 thorpej if (__predict_false((ic->ic_flags & IC_UCMD) != 0))
906 1.8 thorpej SIMPLEQ_REMOVE_HEAD(&icp->icp_ucmd_queue, ic_chain);
907 1.8 thorpej else
908 1.8 thorpej SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_queue, ic_chain);
909 1.1 ad }
910 1.1 ad
911 1.1 ad splx(s);
912 1.1 ad }
913 1.1 ad
914 1.1 ad int
915 1.1 ad icp_ccb_map(struct icp_softc *icp, struct icp_ccb *ic, void *data, int size,
916 1.1 ad int dir)
917 1.1 ad {
918 1.1 ad struct icp_sg *sg;
919 1.1 ad int nsegs, i, rv;
920 1.1 ad bus_dmamap_t xfer;
921 1.1 ad
922 1.1 ad xfer = ic->ic_xfer_map;
923 1.1 ad
924 1.1 ad rv = bus_dmamap_load(icp->icp_dmat, xfer, data, size, NULL,
925 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
926 1.1 ad ((dir & IC_XFER_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
927 1.1 ad if (rv != 0)
928 1.1 ad return (rv);
929 1.1 ad
930 1.1 ad nsegs = xfer->dm_nsegs;
931 1.1 ad ic->ic_xfer_size = size;
932 1.1 ad ic->ic_nsgent = nsegs;
933 1.1 ad ic->ic_flags |= dir;
934 1.1 ad sg = ic->ic_sg;
935 1.1 ad
936 1.1 ad if (sg != NULL) {
937 1.1 ad for (i = 0; i < nsegs; i++, sg++) {
938 1.1 ad sg->sg_addr = htole32(xfer->dm_segs[i].ds_addr);
939 1.1 ad sg->sg_len = htole32(xfer->dm_segs[i].ds_len);
940 1.1 ad }
941 1.1 ad } else if (nsegs > 1)
942 1.4 provos panic("icp_ccb_map: no SG list specified, but nsegs > 1");
943 1.1 ad
944 1.1 ad if ((dir & IC_XFER_OUT) != 0)
945 1.1 ad i = BUS_DMASYNC_PREWRITE;
946 1.1 ad else /* if ((dir & IC_XFER_IN) != 0) */
947 1.1 ad i = BUS_DMASYNC_PREREAD;
948 1.1 ad
949 1.1 ad bus_dmamap_sync(icp->icp_dmat, xfer, 0, ic->ic_xfer_size, i);
950 1.1 ad return (0);
951 1.1 ad }
952 1.1 ad
953 1.1 ad void
954 1.1 ad icp_ccb_unmap(struct icp_softc *icp, struct icp_ccb *ic)
955 1.1 ad {
956 1.1 ad int i;
957 1.1 ad
958 1.1 ad if ((ic->ic_flags & IC_XFER_OUT) != 0)
959 1.1 ad i = BUS_DMASYNC_POSTWRITE;
960 1.1 ad else /* if ((ic->ic_flags & IC_XFER_IN) != 0) */
961 1.1 ad i = BUS_DMASYNC_POSTREAD;
962 1.1 ad
963 1.1 ad bus_dmamap_sync(icp->icp_dmat, ic->ic_xfer_map, 0, ic->ic_xfer_size, i);
964 1.1 ad bus_dmamap_unload(icp->icp_dmat, ic->ic_xfer_map);
965 1.1 ad }
966 1.1 ad
967 1.1 ad int
968 1.1 ad icp_ccb_poll(struct icp_softc *icp, struct icp_ccb *ic, int timo)
969 1.1 ad {
970 1.1 ad int rv;
971 1.1 ad
972 1.1 ad for (timo = ICP_BUSY_WAIT_MS * 100; timo != 0; timo--) {
973 1.1 ad if (!(*icp->icp_test_busy)(icp))
974 1.1 ad break;
975 1.1 ad DELAY(10);
976 1.1 ad }
977 1.1 ad if (timo == 0) {
978 1.1 ad printf("%s: submit: busy\n", icp->icp_dv.dv_xname);
979 1.1 ad return (EAGAIN);
980 1.1 ad }
981 1.1 ad
982 1.1 ad icp_ccb_submit(icp, ic);
983 1.1 ad
984 1.1 ad for (timo *= 10; timo != 0; timo--) {
985 1.1 ad DELAY(100);
986 1.1 ad icp_intr(icp);
987 1.1 ad if ((ic->ic_flags & IC_COMPLETE) != 0)
988 1.1 ad break;
989 1.1 ad }
990 1.1 ad
991 1.1 ad if (timo != 0) {
992 1.1 ad if (ic->ic_status != ICP_S_OK) {
993 1.1 ad #ifdef ICP_DEBUG
994 1.1 ad printf("%s: request failed; status=0x%04x\n",
995 1.1 ad icp->icp_dv.dv_xname, ic->ic_status);
996 1.1 ad #endif
997 1.1 ad rv = EIO;
998 1.1 ad } else
999 1.1 ad rv = 0;
1000 1.1 ad } else {
1001 1.1 ad printf("%s: command timed out\n", icp->icp_dv.dv_xname);
1002 1.1 ad rv = EIO;
1003 1.1 ad }
1004 1.1 ad
1005 1.1 ad while ((*icp->icp_test_busy)(icp) != 0)
1006 1.1 ad DELAY(10);
1007 1.1 ad
1008 1.1 ad return (rv);
1009 1.1 ad }
1010 1.1 ad
1011 1.1 ad int
1012 1.1 ad icp_ccb_wait(struct icp_softc *icp, struct icp_ccb *ic, int timo)
1013 1.1 ad {
1014 1.1 ad int s, rv;
1015 1.1 ad
1016 1.1 ad ic->ic_flags |= IC_WAITING;
1017 1.1 ad
1018 1.1 ad s = splbio();
1019 1.1 ad icp_ccb_enqueue(icp, ic);
1020 1.8 thorpej while ((ic->ic_flags & IC_COMPLETE) == 0) {
1021 1.8 thorpej if ((rv = tsleep(ic, PRIBIO, "icpwccb", mstohz(timo))) != 0) {
1022 1.8 thorpej splx(s);
1023 1.8 thorpej return (rv);
1024 1.8 thorpej }
1025 1.1 ad }
1026 1.1 ad splx(s);
1027 1.1 ad
1028 1.1 ad if (ic->ic_status != ICP_S_OK) {
1029 1.1 ad printf("%s: command failed; status=%x\n", icp->icp_dv.dv_xname,
1030 1.1 ad ic->ic_status);
1031 1.1 ad return (EIO);
1032 1.1 ad }
1033 1.1 ad
1034 1.1 ad return (0);
1035 1.1 ad }
1036 1.1 ad
1037 1.8 thorpej int
1038 1.8 thorpej icp_ccb_wait_user(struct icp_softc *icp, struct icp_ccb *ic, int timo)
1039 1.8 thorpej {
1040 1.8 thorpej int s, rv;
1041 1.8 thorpej
1042 1.8 thorpej ic->ic_dv = &icp->icp_dv;
1043 1.8 thorpej ic->ic_intr = icp_ucmd_intr;
1044 1.8 thorpej ic->ic_flags |= IC_UCMD;
1045 1.8 thorpej
1046 1.8 thorpej s = splbio();
1047 1.8 thorpej icp_ccb_enqueue(icp, ic);
1048 1.8 thorpej while ((ic->ic_flags & IC_COMPLETE) == 0) {
1049 1.8 thorpej if ((rv = tsleep(ic, PRIBIO, "icpwuccb", mstohz(timo))) != 0) {
1050 1.8 thorpej splx(s);
1051 1.8 thorpej return (rv);
1052 1.8 thorpej }
1053 1.8 thorpej }
1054 1.8 thorpej splx(s);
1055 1.8 thorpej
1056 1.8 thorpej return (0);
1057 1.8 thorpej }
1058 1.8 thorpej
1059 1.1 ad void
1060 1.1 ad icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic)
1061 1.1 ad {
1062 1.1 ad
1063 1.1 ad ic->ic_cmdlen = (ic->ic_cmdlen + 3) & ~3;
1064 1.1 ad
1065 1.1 ad (*icp->icp_set_sema0)(icp);
1066 1.1 ad DELAY(10);
1067 1.1 ad
1068 1.1 ad ic->ic_cmd.cmd_boardnode = htole32(ICP_LOCALBOARD);
1069 1.1 ad ic->ic_cmd.cmd_cmdindex = htole32(ic->ic_ident);
1070 1.1 ad
1071 1.1 ad (*icp->icp_copy_cmd)(icp, ic);
1072 1.1 ad (*icp->icp_release_event)(icp, ic);
1073 1.8 thorpej }
1074 1.8 thorpej
1075 1.8 thorpej /* XXX Global - should be per-controller? XXX */
1076 1.8 thorpej static gdt_evt_str icp_event_buffer[ICP_MAX_EVENTS];
1077 1.8 thorpej static int icp_event_oldidx;
1078 1.8 thorpej static int icp_event_lastidx;
1079 1.8 thorpej
1080 1.8 thorpej gdt_evt_str *
1081 1.8 thorpej icp_store_event(struct icp_softc *icp, u_int16_t source, u_int16_t idx,
1082 1.8 thorpej gdt_evt_data *evt)
1083 1.8 thorpej {
1084 1.8 thorpej gdt_evt_str *e;
1085 1.8 thorpej
1086 1.8 thorpej /* no source == no event */
1087 1.8 thorpej if (source == 0)
1088 1.8 thorpej return (NULL);
1089 1.8 thorpej
1090 1.8 thorpej e = &icp_event_buffer[icp_event_lastidx];
1091 1.8 thorpej if (e->event_source == source && e->event_idx == idx &&
1092 1.8 thorpej ((evt->size != 0 && e->event_data.size != 0 &&
1093 1.8 thorpej memcmp(&e->event_data.eu, &evt->eu, evt->size) == 0) ||
1094 1.8 thorpej (evt->size == 0 && e->event_data.size == 0 &&
1095 1.8 thorpej strcmp((char *) e->event_data.event_string,
1096 1.8 thorpej (char *) evt->event_string) == 0))) {
1097 1.8 thorpej e->last_stamp = time.tv_sec;
1098 1.8 thorpej e->same_count++;
1099 1.8 thorpej } else {
1100 1.8 thorpej if (icp_event_buffer[icp_event_lastidx].event_source != 0) {
1101 1.8 thorpej icp_event_lastidx++;
1102 1.8 thorpej if (icp_event_lastidx == ICP_MAX_EVENTS)
1103 1.8 thorpej icp_event_lastidx = 0;
1104 1.8 thorpej if (icp_event_lastidx == icp_event_oldidx) {
1105 1.8 thorpej icp_event_oldidx++;
1106 1.8 thorpej if (icp_event_oldidx == ICP_MAX_EVENTS)
1107 1.8 thorpej icp_event_oldidx = 0;
1108 1.8 thorpej }
1109 1.8 thorpej }
1110 1.8 thorpej e = &icp_event_buffer[icp_event_lastidx];
1111 1.8 thorpej e->event_source = source;
1112 1.8 thorpej e->event_idx = idx;
1113 1.8 thorpej e->first_stamp = e->last_stamp = time.tv_sec;
1114 1.8 thorpej e->same_count = 1;
1115 1.8 thorpej e->event_data = *evt;
1116 1.8 thorpej e->application = 0;
1117 1.8 thorpej }
1118 1.8 thorpej return (e);
1119 1.8 thorpej }
1120 1.8 thorpej
1121 1.8 thorpej int
1122 1.8 thorpej icp_read_event(struct icp_softc *icp, int handle, gdt_evt_str *estr)
1123 1.8 thorpej {
1124 1.8 thorpej gdt_evt_str *e;
1125 1.8 thorpej int eindex, s;
1126 1.8 thorpej
1127 1.8 thorpej s = splbio();
1128 1.8 thorpej
1129 1.8 thorpej if (handle == -1)
1130 1.8 thorpej eindex = icp_event_oldidx;
1131 1.8 thorpej else
1132 1.8 thorpej eindex = handle;
1133 1.8 thorpej
1134 1.8 thorpej estr->event_source = 0;
1135 1.8 thorpej
1136 1.8 thorpej if (eindex < 0 || eindex >= ICP_MAX_EVENTS) {
1137 1.8 thorpej splx(s);
1138 1.8 thorpej return (eindex);
1139 1.8 thorpej }
1140 1.8 thorpej
1141 1.8 thorpej e = &icp_event_buffer[eindex];
1142 1.8 thorpej if (e->event_source != 0) {
1143 1.8 thorpej if (eindex != icp_event_lastidx) {
1144 1.8 thorpej eindex++;
1145 1.8 thorpej if (eindex == ICP_MAX_EVENTS)
1146 1.8 thorpej eindex = 0;
1147 1.8 thorpej } else
1148 1.8 thorpej eindex = -1;
1149 1.8 thorpej memcpy(estr, e, sizeof(gdt_evt_str));
1150 1.8 thorpej }
1151 1.8 thorpej
1152 1.8 thorpej splx(s);
1153 1.8 thorpej
1154 1.8 thorpej return (eindex);
1155 1.8 thorpej }
1156 1.8 thorpej
1157 1.8 thorpej void
1158 1.8 thorpej icp_readapp_event(struct icp_softc *icp, u_int8_t application,
1159 1.8 thorpej gdt_evt_str *estr)
1160 1.8 thorpej {
1161 1.8 thorpej gdt_evt_str *e;
1162 1.8 thorpej int found = 0, eindex, s;
1163 1.8 thorpej
1164 1.8 thorpej s = splbio();
1165 1.8 thorpej
1166 1.8 thorpej eindex = icp_event_oldidx;
1167 1.8 thorpej for (;;) {
1168 1.8 thorpej e = &icp_event_buffer[eindex];
1169 1.8 thorpej if (e->event_source == 0)
1170 1.8 thorpej break;
1171 1.8 thorpej if ((e->application & application) == 0) {
1172 1.8 thorpej e->application |= application;
1173 1.8 thorpej found = 1;
1174 1.8 thorpej break;
1175 1.8 thorpej }
1176 1.8 thorpej if (eindex == icp_event_lastidx)
1177 1.8 thorpej break;
1178 1.8 thorpej eindex++;
1179 1.8 thorpej if (eindex == ICP_MAX_EVENTS)
1180 1.8 thorpej eindex = 0;
1181 1.8 thorpej }
1182 1.8 thorpej if (found)
1183 1.8 thorpej memcpy(estr, e, sizeof(gdt_evt_str));
1184 1.8 thorpej else
1185 1.8 thorpej estr->event_source = 0;
1186 1.8 thorpej
1187 1.8 thorpej splx(s);
1188 1.8 thorpej }
1189 1.8 thorpej
1190 1.8 thorpej void
1191 1.8 thorpej icp_clear_events(struct icp_softc *icp)
1192 1.8 thorpej {
1193 1.8 thorpej int s;
1194 1.8 thorpej
1195 1.8 thorpej s = splbio();
1196 1.8 thorpej icp_event_oldidx = icp_event_lastidx = 0;
1197 1.8 thorpej memset(icp_event_buffer, 0, sizeof(icp_event_buffer));
1198 1.8 thorpej splx(s);
1199 1.1 ad }
1200