icp.c revision 1.7 1 1.7 thorpej /* $NetBSD: icp.c,v 1.7 2003/01/31 00:26:30 thorpej Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2002 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.1 ad * by Andrew Doran.
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.1 ad */
77 1.1 ad
78 1.1 ad #include <sys/cdefs.h>
79 1.7 thorpej __KERNEL_RCSID(0, "$NetBSD: icp.c,v 1.7 2003/01/31 00:26:30 thorpej Exp $");
80 1.1 ad
81 1.1 ad #include <sys/param.h>
82 1.1 ad #include <sys/systm.h>
83 1.1 ad #include <sys/kernel.h>
84 1.1 ad #include <sys/device.h>
85 1.1 ad #include <sys/queue.h>
86 1.1 ad #include <sys/proc.h>
87 1.1 ad #include <sys/buf.h>
88 1.1 ad #include <sys/endian.h>
89 1.1 ad #include <sys/malloc.h>
90 1.1 ad #include <sys/disk.h>
91 1.1 ad
92 1.1 ad #include <uvm/uvm_extern.h>
93 1.1 ad
94 1.1 ad #include <machine/bswap.h>
95 1.1 ad #include <machine/bus.h>
96 1.1 ad
97 1.1 ad #include <dev/pci/pcireg.h>
98 1.1 ad #include <dev/pci/pcivar.h>
99 1.1 ad #include <dev/pci/pcidevs.h>
100 1.1 ad
101 1.1 ad #include <dev/ic/icpreg.h>
102 1.1 ad #include <dev/ic/icpvar.h>
103 1.1 ad
104 1.1 ad int icp_async_event(struct icp_softc *, int);
105 1.1 ad void icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic);
106 1.1 ad void icp_chain(struct icp_softc *);
107 1.1 ad int icp_print(void *, const char *);
108 1.1 ad int icp_submatch(struct device *, struct cfdata *, void *);
109 1.1 ad void icp_watchdog(void *);
110 1.1 ad
111 1.1 ad int
112 1.1 ad icp_init(struct icp_softc *icp, const char *intrstr)
113 1.1 ad {
114 1.1 ad struct icp_attach_args icpa;
115 1.1 ad struct icp_binfo binfo;
116 1.1 ad struct icp_ccb *ic;
117 1.1 ad u_int16_t cdev_cnt;
118 1.1 ad int i, j, state, feat, nsegs, rv, noscsi, nocache;
119 1.1 ad
120 1.5 simonb state = 0;
121 1.1 ad noscsi = 0;
122 1.1 ad nocache = 0;
123 1.1 ad
124 1.1 ad if (intrstr != NULL)
125 1.7 thorpej aprint_normal("%s: interrupting at %s\n", icp->icp_dv.dv_xname,
126 1.1 ad intrstr);
127 1.1 ad
128 1.1 ad SIMPLEQ_INIT(&icp->icp_ccb_queue);
129 1.1 ad SIMPLEQ_INIT(&icp->icp_ccb_freelist);
130 1.1 ad callout_init(&icp->icp_wdog_callout);
131 1.1 ad
132 1.1 ad /*
133 1.1 ad * Allocate a scratch area.
134 1.1 ad */
135 1.1 ad if (bus_dmamap_create(icp->icp_dmat, ICP_SCRATCH_SIZE, 1,
136 1.1 ad ICP_SCRATCH_SIZE, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
137 1.1 ad &icp->icp_scr_dmamap) != 0) {
138 1.7 thorpej aprint_error("%s: cannot create scratch dmamap\n",
139 1.1 ad icp->icp_dv.dv_xname);
140 1.1 ad return (1);
141 1.1 ad }
142 1.1 ad state++;
143 1.1 ad
144 1.1 ad if (bus_dmamem_alloc(icp->icp_dmat, ICP_SCRATCH_SIZE, PAGE_SIZE, 0,
145 1.1 ad icp->icp_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
146 1.7 thorpej aprint_error("%s: cannot alloc scratch dmamem\n",
147 1.1 ad icp->icp_dv.dv_xname);
148 1.1 ad goto bail_out;
149 1.1 ad }
150 1.1 ad state++;
151 1.1 ad
152 1.1 ad if (bus_dmamem_map(icp->icp_dmat, icp->icp_scr_seg, nsegs,
153 1.1 ad ICP_SCRATCH_SIZE, &icp->icp_scr, 0)) {
154 1.7 thorpej aprint_error("%s: cannot map scratch dmamem\n",
155 1.7 thorpej icp->icp_dv.dv_xname);
156 1.1 ad goto bail_out;
157 1.1 ad }
158 1.1 ad state++;
159 1.1 ad
160 1.1 ad if (bus_dmamap_load(icp->icp_dmat, icp->icp_scr_dmamap, icp->icp_scr,
161 1.1 ad ICP_SCRATCH_SIZE, NULL, BUS_DMA_NOWAIT)) {
162 1.7 thorpej aprint_error("%s: cannot load scratch dmamap\n",
163 1.7 thorpej icp->icp_dv.dv_xname);
164 1.1 ad goto bail_out;
165 1.1 ad }
166 1.1 ad state++;
167 1.1 ad
168 1.1 ad /*
169 1.1 ad * Allocate and initialize the command control blocks.
170 1.1 ad */
171 1.1 ad ic = malloc(sizeof(*ic) * ICP_NCCBS, M_DEVBUF, M_NOWAIT | M_ZERO);
172 1.1 ad if ((icp->icp_ccbs = ic) == NULL) {
173 1.7 thorpej aprint_error("%s: malloc() failed\n", icp->icp_dv.dv_xname);
174 1.1 ad goto bail_out;
175 1.1 ad }
176 1.1 ad state++;
177 1.1 ad
178 1.1 ad for (i = 0; i < ICP_NCCBS; i++, ic++) {
179 1.1 ad /*
180 1.1 ad * The first two command indexes have special meanings, so
181 1.1 ad * we can't use them.
182 1.1 ad */
183 1.1 ad ic->ic_ident = i + 2;
184 1.1 ad rv = bus_dmamap_create(icp->icp_dmat, ICP_MAX_XFER,
185 1.1 ad ICP_MAXSG, ICP_MAX_XFER, 0,
186 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
187 1.1 ad &ic->ic_xfer_map);
188 1.1 ad if (rv != 0)
189 1.1 ad break;
190 1.1 ad icp->icp_nccbs++;
191 1.1 ad icp_ccb_free(icp, ic);
192 1.1 ad }
193 1.1 ad #ifdef DIAGNOSTIC
194 1.1 ad if (icp->icp_nccbs != ICP_NCCBS)
195 1.7 thorpej aprint_error("%s: %d/%d CCBs usable\n", icp->icp_dv.dv_xname,
196 1.1 ad icp->icp_nccbs, ICP_NCCBS);
197 1.1 ad #endif
198 1.1 ad
199 1.1 ad /*
200 1.1 ad * Initalize the controller.
201 1.1 ad */
202 1.1 ad if (!icp_cmd(icp, ICP_SCREENSERVICE, ICP_INIT, 0, 0, 0)) {
203 1.7 thorpej aprint_error("%s: screen service init error %d\n",
204 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
205 1.1 ad goto bail_out;
206 1.1 ad }
207 1.1 ad
208 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
209 1.7 thorpej aprint_error("%s: cache service init error %d\n",
210 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
211 1.1 ad goto bail_out;
212 1.1 ad }
213 1.1 ad
214 1.1 ad icp_cmd(icp, ICP_CACHESERVICE, ICP_UNFREEZE_IO, 0, 0, 0);
215 1.1 ad
216 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_MOUNT, 0xffff, 1, 0)) {
217 1.7 thorpej aprint_error("%s: cache service mount error %d\n",
218 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
219 1.1 ad goto bail_out;
220 1.1 ad }
221 1.1 ad
222 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INIT, ICP_LINUX_OS, 0, 0)) {
223 1.7 thorpej aprint_error("%s: cache service post-mount init error %d\n",
224 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
225 1.1 ad goto bail_out;
226 1.1 ad }
227 1.1 ad cdev_cnt = (u_int16_t)icp->icp_info;
228 1.1 ad
229 1.1 ad if (!icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_INIT, 0, 0, 0)) {
230 1.7 thorpej aprint_error("%s: raw service init error %d\n",
231 1.1 ad icp->icp_dv.dv_xname, icp->icp_status);
232 1.1 ad goto bail_out;
233 1.1 ad }
234 1.1 ad
235 1.1 ad /*
236 1.1 ad * Set/get raw service features (scatter/gather).
237 1.1 ad */
238 1.1 ad feat = 0;
239 1.1 ad if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_SET_FEAT, ICP_SCATTER_GATHER,
240 1.1 ad 0, 0))
241 1.1 ad if (icp_cmd(icp, ICP_SCSIRAWSERVICE, ICP_GET_FEAT, 0, 0, 0))
242 1.1 ad feat = icp->icp_info;
243 1.1 ad
244 1.1 ad if ((feat & ICP_SCATTER_GATHER) == 0) {
245 1.1 ad #ifdef DIAGNOSTIC
246 1.7 thorpej aprint_normal(
247 1.7 thorpej "%s: scatter/gather not supported (raw service)\n",
248 1.1 ad icp->icp_dv.dv_xname);
249 1.1 ad #endif
250 1.1 ad noscsi = 1;
251 1.1 ad }
252 1.1 ad
253 1.1 ad /*
254 1.1 ad * Set/get cache service features (scatter/gather).
255 1.1 ad */
256 1.1 ad feat = 0;
257 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_SET_FEAT, 0,
258 1.1 ad ICP_SCATTER_GATHER, 0))
259 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_GET_FEAT, 0, 0, 0))
260 1.1 ad feat = icp->icp_info;
261 1.1 ad
262 1.1 ad if ((feat & ICP_SCATTER_GATHER) == 0) {
263 1.1 ad #ifdef DIAGNOSTIC
264 1.7 thorpej aprint_normal(
265 1.7 thorpej "%s: scatter/gather not supported (cache service)\n",
266 1.1 ad icp->icp_dv.dv_xname);
267 1.1 ad #endif
268 1.1 ad nocache = 1;
269 1.1 ad }
270 1.1 ad
271 1.1 ad /*
272 1.1 ad * Pull some information from the board and dump.
273 1.1 ad */
274 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_BOARD_INFO,
275 1.1 ad ICP_INVALID_CHANNEL, sizeof(struct icp_binfo))) {
276 1.7 thorpej aprint_error("%s: unable to retrive board info\n",
277 1.1 ad icp->icp_dv.dv_xname);
278 1.1 ad goto bail_out;
279 1.1 ad }
280 1.1 ad memcpy(&binfo, icp->icp_scr, sizeof(binfo));
281 1.1 ad
282 1.7 thorpej aprint_normal(
283 1.7 thorpej "%s: model <%s>, firmware <%s>, %d channel(s), %dMB memory\n",
284 1.1 ad icp->icp_dv.dv_xname, binfo.bi_type_string, binfo.bi_raid_string,
285 1.1 ad binfo.bi_chan_count, le32toh(binfo.bi_memsize) >> 20);
286 1.1 ad
287 1.1 ad /*
288 1.1 ad * Determine the number of devices, and number of openings per
289 1.1 ad * device.
290 1.1 ad */
291 1.1 ad if (!nocache) {
292 1.1 ad for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
293 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_INFO, j, 0,
294 1.1 ad 0))
295 1.1 ad continue;
296 1.1 ad
297 1.1 ad icp->icp_cdr[j].cd_size = icp->icp_info;
298 1.1 ad icp->icp_ndevs++;
299 1.1 ad
300 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_DEVTYPE, j, 0,
301 1.1 ad 0))
302 1.1 ad icp->icp_cdr[j].cd_type = icp->icp_info;
303 1.1 ad }
304 1.1 ad }
305 1.1 ad
306 1.1 ad if (!noscsi)
307 1.1 ad icp->icp_ndevs += binfo.bi_chan_count;
308 1.1 ad
309 1.1 ad if (icp->icp_ndevs != 0)
310 1.1 ad icp->icp_openings =
311 1.1 ad (icp->icp_nccbs - ICP_NCCB_RESERVE) / icp->icp_ndevs;
312 1.1 ad #ifdef ICP_DEBUG
313 1.7 thorpej aprint_debug("%s: %d openings per device\n", icp->icp_dv.dv_xname,
314 1.1 ad icp->icp_openings);
315 1.1 ad #endif
316 1.1 ad
317 1.1 ad /*
318 1.1 ad * Attach SCSI channels.
319 1.1 ad */
320 1.1 ad if (!noscsi) {
321 1.1 ad struct icp_ioc_version *iv;
322 1.1 ad struct icp_rawioc *ri;
323 1.1 ad struct icp_getch *gc;
324 1.1 ad
325 1.1 ad iv = (struct icp_ioc_version *)icp->icp_scr;
326 1.1 ad iv->iv_version = htole32(ICP_IOC_NEWEST);
327 1.1 ad iv->iv_listents = ICP_MAXBUS;
328 1.1 ad iv->iv_firstchan = 0;
329 1.1 ad iv->iv_lastchan = ICP_MAXBUS - 1;
330 1.1 ad iv->iv_listoffset = htole32(sizeof(*iv));
331 1.1 ad
332 1.1 ad if (icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
333 1.1 ad ICP_IOCHAN_RAW_DESC, ICP_INVALID_CHANNEL,
334 1.1 ad sizeof(*iv) + ICP_MAXBUS * sizeof(*ri))) {
335 1.1 ad ri = (struct icp_rawioc *)(iv + 1);
336 1.1 ad for (j = 0; j < binfo.bi_chan_count; j++, ri++)
337 1.1 ad icp->icp_bus_id[j] = ri->ri_procid;
338 1.1 ad } else {
339 1.1 ad /*
340 1.1 ad * Fall back to the old method.
341 1.1 ad */
342 1.1 ad gc = (struct icp_getch *)icp->icp_scr;
343 1.1 ad
344 1.1 ad for (i = 0; j < binfo.bi_chan_count; j++) {
345 1.1 ad if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL,
346 1.1 ad ICP_SCSI_CHAN_CNT | ICP_L_CTRL_PATTERN,
347 1.1 ad ICP_IO_CHANNEL | ICP_INVALID_CHANNEL,
348 1.1 ad sizeof(*gc))) {
349 1.7 thorpej aprint_error(
350 1.7 thorpej "%s: unable to get chan info",
351 1.1 ad icp->icp_dv.dv_xname);
352 1.1 ad goto bail_out;
353 1.1 ad }
354 1.1 ad icp->icp_bus_id[j] = gc->gc_scsiid;
355 1.1 ad }
356 1.1 ad }
357 1.1 ad
358 1.1 ad for (j = 0; j < binfo.bi_chan_count; j++) {
359 1.1 ad if (icp->icp_bus_id[j] > ICP_MAXID_FC)
360 1.1 ad icp->icp_bus_id[j] = ICP_MAXID_FC;
361 1.1 ad
362 1.1 ad icpa.icpa_unit = j + ICPA_UNIT_SCSI;
363 1.1 ad config_found_sm(&icp->icp_dv, &icpa, icp_print,
364 1.1 ad icp_submatch);
365 1.1 ad }
366 1.1 ad }
367 1.1 ad
368 1.1 ad /*
369 1.1 ad * Attach cache devices.
370 1.1 ad */
371 1.1 ad if (!nocache) {
372 1.1 ad for (j = 0; j < cdev_cnt && j < ICP_MAX_HDRIVES; j++) {
373 1.1 ad if (icp->icp_cdr[j].cd_size == 0)
374 1.1 ad continue;
375 1.1 ad
376 1.1 ad icpa.icpa_unit = j;
377 1.1 ad config_found_sm(&icp->icp_dv, &icpa, icp_print,
378 1.1 ad icp_submatch);
379 1.1 ad }
380 1.1 ad }
381 1.1 ad
382 1.1 ad /*
383 1.1 ad * Start the watchdog.
384 1.1 ad */
385 1.1 ad icp_watchdog(icp);
386 1.1 ad return (0);
387 1.1 ad
388 1.1 ad bail_out:
389 1.1 ad if (state > 4)
390 1.1 ad for (j = 0; j < i; j++)
391 1.1 ad bus_dmamap_destroy(icp->icp_dmat,
392 1.1 ad icp->icp_ccbs[j].ic_xfer_map);
393 1.1 ad if (state > 3)
394 1.1 ad free(icp->icp_ccbs, M_DEVBUF);
395 1.1 ad if (state > 2)
396 1.1 ad bus_dmamap_unload(icp->icp_dmat, icp->icp_scr_dmamap);
397 1.1 ad if (state > 1)
398 1.1 ad bus_dmamem_unmap(icp->icp_dmat, icp->icp_scr,
399 1.1 ad ICP_SCRATCH_SIZE);
400 1.1 ad if (state > 0)
401 1.1 ad bus_dmamem_free(icp->icp_dmat, icp->icp_scr_seg, nsegs);
402 1.1 ad bus_dmamap_destroy(icp->icp_dmat, icp->icp_scr_dmamap);
403 1.1 ad
404 1.1 ad return (1);
405 1.1 ad }
406 1.1 ad
407 1.1 ad void
408 1.1 ad icp_watchdog(void *cookie)
409 1.1 ad {
410 1.1 ad struct icp_softc *icp;
411 1.1 ad int s;
412 1.1 ad
413 1.1 ad icp = cookie;
414 1.1 ad
415 1.1 ad s = splbio();
416 1.1 ad icp_intr(icp);
417 1.2 lukem if (! SIMPLEQ_EMPTY(&icp->icp_ccb_queue))
418 1.1 ad icp_ccb_enqueue(icp, NULL);
419 1.1 ad splx(s);
420 1.1 ad
421 1.1 ad callout_reset(&icp->icp_wdog_callout, hz * ICP_WATCHDOG_FREQ,
422 1.1 ad icp_watchdog, icp);
423 1.1 ad }
424 1.1 ad
425 1.1 ad int
426 1.1 ad icp_print(void *aux, const char *pnp)
427 1.1 ad {
428 1.1 ad struct icp_attach_args *icpa;
429 1.1 ad const char *str;
430 1.1 ad
431 1.1 ad icpa = (struct icp_attach_args *)aux;
432 1.1 ad
433 1.1 ad if (pnp != NULL) {
434 1.1 ad if (icpa->icpa_unit < ICPA_UNIT_SCSI)
435 1.1 ad str = "block device";
436 1.1 ad else
437 1.1 ad str = "SCSI channel";
438 1.6 thorpej aprint_normal("%s at %s", str, pnp);
439 1.1 ad }
440 1.6 thorpej aprint_normal(" unit %d", icpa->icpa_unit);
441 1.1 ad
442 1.1 ad return (UNCONF);
443 1.1 ad }
444 1.1 ad
445 1.1 ad int
446 1.1 ad icp_submatch(struct device *parent, struct cfdata *cf, void *aux)
447 1.1 ad {
448 1.1 ad struct icp_attach_args *icpa;
449 1.1 ad
450 1.1 ad icpa = (struct icp_attach_args *)aux;
451 1.1 ad
452 1.1 ad if (cf->icpacf_unit != ICPCF_UNIT_DEFAULT &&
453 1.1 ad cf->icpacf_unit != icpa->icpa_unit)
454 1.1 ad return (0);
455 1.1 ad
456 1.3 thorpej return (config_match(parent, cf, aux));
457 1.1 ad }
458 1.1 ad
459 1.1 ad int
460 1.1 ad icp_async_event(struct icp_softc *icp, int val)
461 1.1 ad {
462 1.1 ad
463 1.1 ad /* XXX */
464 1.1 ad return (1);
465 1.1 ad }
466 1.1 ad
467 1.1 ad int
468 1.1 ad icp_intr(void *cookie)
469 1.1 ad {
470 1.1 ad struct icp_softc *icp;
471 1.1 ad struct icp_intr_ctx ctx;
472 1.1 ad struct icp_ccb *ic;
473 1.1 ad
474 1.1 ad icp = cookie;
475 1.1 ad
476 1.1 ad ctx.istatus = (*icp->icp_get_status)(icp);
477 1.1 ad if (!ctx.istatus) {
478 1.1 ad icp->icp_status = ICP_S_NO_STATUS;
479 1.1 ad return (0);
480 1.1 ad }
481 1.1 ad
482 1.1 ad (*icp->icp_intr)(icp, &ctx);
483 1.1 ad
484 1.1 ad icp->icp_status = ctx.cmd_status;
485 1.1 ad icp->icp_info = ctx.info;
486 1.1 ad icp->icp_info2 = ctx.info2;
487 1.1 ad
488 1.1 ad switch (ctx.istatus) {
489 1.1 ad case ICP_ASYNCINDEX:
490 1.1 ad icp_async_event(icp, ctx.service);
491 1.1 ad return (1);
492 1.1 ad
493 1.1 ad case ICP_SPEZINDEX:
494 1.1 ad printf("%s: uninitialized or unknown service (%d/%d)\n",
495 1.1 ad icp->icp_dv.dv_xname, ctx.info, ctx.info2);
496 1.1 ad return (1);
497 1.1 ad }
498 1.1 ad
499 1.1 ad if ((ctx.istatus - 2) > icp->icp_nccbs)
500 1.1 ad panic("icp_intr: bad command index returned");
501 1.1 ad
502 1.1 ad ic = &icp->icp_ccbs[ctx.istatus - 2];
503 1.1 ad ic->ic_status = icp->icp_status;
504 1.1 ad
505 1.1 ad if ((ic->ic_flags & IC_ALLOCED) == 0)
506 1.1 ad panic("icp_intr: inactive CCB identified");
507 1.1 ad
508 1.1 ad switch (icp->icp_status) {
509 1.1 ad case ICP_S_BSY:
510 1.1 ad #ifdef ICP_DEBUG
511 1.1 ad printf("%s: ICP_S_BSY received\n", icp->icp_dv.dv_xname);
512 1.1 ad #endif
513 1.1 ad SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_queue, ic, ic_chain);
514 1.1 ad break;
515 1.1 ad
516 1.1 ad default:
517 1.1 ad ic->ic_flags |= IC_COMPLETE;
518 1.1 ad
519 1.1 ad if ((ic->ic_flags & IC_WAITING) != 0)
520 1.1 ad wakeup(ic);
521 1.1 ad else if (ic->ic_intr != NULL)
522 1.1 ad (*ic->ic_intr)(ic);
523 1.1 ad
524 1.2 lukem if (! SIMPLEQ_EMPTY(&icp->icp_ccb_queue))
525 1.1 ad icp_ccb_enqueue(icp, NULL);
526 1.1 ad
527 1.1 ad break;
528 1.1 ad }
529 1.1 ad
530 1.1 ad return (1);
531 1.1 ad }
532 1.1 ad
533 1.1 ad int
534 1.1 ad icp_cmd(struct icp_softc *icp, u_int8_t service, u_int16_t opcode,
535 1.1 ad u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
536 1.1 ad {
537 1.1 ad struct icp_ioctlcmd *icmd;
538 1.1 ad struct icp_cachecmd *cc;
539 1.1 ad struct icp_rawcmd *rc;
540 1.1 ad int retries, rv;
541 1.1 ad struct icp_ccb *ic;
542 1.1 ad
543 1.1 ad retries = ICP_RETRIES;
544 1.1 ad
545 1.1 ad do {
546 1.1 ad ic = icp_ccb_alloc(icp);
547 1.1 ad memset(&ic->ic_cmd, 0, sizeof(ic->ic_cmd));
548 1.1 ad ic->ic_cmd.cmd_opcode = htole16(opcode);
549 1.1 ad
550 1.1 ad switch (service) {
551 1.1 ad case ICP_CACHESERVICE:
552 1.1 ad if (opcode == ICP_IOCTL) {
553 1.1 ad icmd = &ic->ic_cmd.cmd_packet.ic;
554 1.1 ad icmd->ic_subfunc = htole16(arg1);
555 1.1 ad icmd->ic_channel = htole32(arg2);
556 1.1 ad icmd->ic_bufsize = htole32(arg3);
557 1.1 ad icmd->ic_addr =
558 1.1 ad htole32(icp->icp_scr_seg[0].ds_addr);
559 1.1 ad
560 1.1 ad bus_dmamap_sync(icp->icp_dmat,
561 1.1 ad icp->icp_scr_dmamap, 0, arg3,
562 1.1 ad BUS_DMASYNC_PREWRITE |
563 1.1 ad BUS_DMASYNC_PREREAD);
564 1.1 ad } else {
565 1.1 ad cc = &ic->ic_cmd.cmd_packet.cc;
566 1.1 ad cc->cc_deviceno = htole16(arg1);
567 1.1 ad cc->cc_blockno = htole32(arg2);
568 1.1 ad }
569 1.1 ad break;
570 1.1 ad
571 1.1 ad case ICP_SCSIRAWSERVICE:
572 1.1 ad rc = &ic->ic_cmd.cmd_packet.rc;
573 1.1 ad rc->rc_direction = htole32(arg1);
574 1.1 ad rc->rc_bus = arg2;
575 1.1 ad rc->rc_target = arg3;
576 1.1 ad rc->rc_lun = arg3 >> 8;
577 1.1 ad break;
578 1.1 ad }
579 1.1 ad
580 1.1 ad ic->ic_service = service;
581 1.1 ad ic->ic_cmdlen = sizeof(ic->ic_cmd);
582 1.1 ad rv = icp_ccb_poll(icp, ic, 10000);
583 1.1 ad
584 1.1 ad switch (service) {
585 1.1 ad case ICP_CACHESERVICE:
586 1.1 ad if (opcode == ICP_IOCTL) {
587 1.1 ad bus_dmamap_sync(icp->icp_dmat,
588 1.1 ad icp->icp_scr_dmamap, 0, arg3,
589 1.1 ad BUS_DMASYNC_POSTWRITE |
590 1.1 ad BUS_DMASYNC_POSTREAD);
591 1.1 ad }
592 1.1 ad break;
593 1.1 ad }
594 1.1 ad
595 1.1 ad icp_ccb_free(icp, ic);
596 1.1 ad } while (rv != 0 && --retries > 0);
597 1.1 ad
598 1.1 ad return (icp->icp_status == ICP_S_OK);
599 1.1 ad }
600 1.1 ad
601 1.1 ad struct icp_ccb *
602 1.1 ad icp_ccb_alloc(struct icp_softc *icp)
603 1.1 ad {
604 1.1 ad struct icp_ccb *ic;
605 1.1 ad int s;
606 1.1 ad
607 1.1 ad s = splbio();
608 1.1 ad ic = SIMPLEQ_FIRST(&icp->icp_ccb_freelist);
609 1.2 lukem SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_freelist, ic_chain);
610 1.1 ad splx(s);
611 1.1 ad
612 1.1 ad ic->ic_flags = IC_ALLOCED;
613 1.1 ad return (ic);
614 1.1 ad }
615 1.1 ad
616 1.1 ad void
617 1.1 ad icp_ccb_free(struct icp_softc *icp, struct icp_ccb *ic)
618 1.1 ad {
619 1.1 ad int s;
620 1.1 ad
621 1.1 ad s = splbio();
622 1.1 ad ic->ic_flags = 0;
623 1.1 ad ic->ic_intr = NULL;
624 1.1 ad SIMPLEQ_INSERT_HEAD(&icp->icp_ccb_freelist, ic, ic_chain);
625 1.1 ad splx(s);
626 1.1 ad }
627 1.1 ad
628 1.1 ad void
629 1.1 ad icp_ccb_enqueue(struct icp_softc *icp, struct icp_ccb *ic)
630 1.1 ad {
631 1.1 ad int s;
632 1.1 ad
633 1.1 ad s = splbio();
634 1.1 ad
635 1.1 ad if (ic != NULL)
636 1.1 ad SIMPLEQ_INSERT_TAIL(&icp->icp_ccb_queue, ic, ic_chain);
637 1.1 ad
638 1.1 ad while ((ic = SIMPLEQ_FIRST(&icp->icp_ccb_queue)) != NULL) {
639 1.1 ad if ((*icp->icp_test_busy)(icp))
640 1.1 ad break;
641 1.1 ad icp_ccb_submit(icp, ic);
642 1.2 lukem SIMPLEQ_REMOVE_HEAD(&icp->icp_ccb_queue, ic_chain);
643 1.1 ad }
644 1.1 ad
645 1.1 ad splx(s);
646 1.1 ad }
647 1.1 ad
648 1.1 ad int
649 1.1 ad icp_ccb_map(struct icp_softc *icp, struct icp_ccb *ic, void *data, int size,
650 1.1 ad int dir)
651 1.1 ad {
652 1.1 ad struct icp_sg *sg;
653 1.1 ad int nsegs, i, rv;
654 1.1 ad bus_dmamap_t xfer;
655 1.1 ad
656 1.1 ad xfer = ic->ic_xfer_map;
657 1.1 ad
658 1.1 ad rv = bus_dmamap_load(icp->icp_dmat, xfer, data, size, NULL,
659 1.1 ad BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
660 1.1 ad ((dir & IC_XFER_IN) ? BUS_DMA_READ : BUS_DMA_WRITE));
661 1.1 ad if (rv != 0)
662 1.1 ad return (rv);
663 1.1 ad
664 1.1 ad nsegs = xfer->dm_nsegs;
665 1.1 ad ic->ic_xfer_size = size;
666 1.1 ad ic->ic_nsgent = nsegs;
667 1.1 ad ic->ic_flags |= dir;
668 1.1 ad sg = ic->ic_sg;
669 1.1 ad
670 1.1 ad if (sg != NULL) {
671 1.1 ad for (i = 0; i < nsegs; i++, sg++) {
672 1.1 ad sg->sg_addr = htole32(xfer->dm_segs[i].ds_addr);
673 1.1 ad sg->sg_len = htole32(xfer->dm_segs[i].ds_len);
674 1.1 ad }
675 1.1 ad } else if (nsegs > 1)
676 1.4 provos panic("icp_ccb_map: no SG list specified, but nsegs > 1");
677 1.1 ad
678 1.1 ad if ((dir & IC_XFER_OUT) != 0)
679 1.1 ad i = BUS_DMASYNC_PREWRITE;
680 1.1 ad else /* if ((dir & IC_XFER_IN) != 0) */
681 1.1 ad i = BUS_DMASYNC_PREREAD;
682 1.1 ad
683 1.1 ad bus_dmamap_sync(icp->icp_dmat, xfer, 0, ic->ic_xfer_size, i);
684 1.1 ad return (0);
685 1.1 ad }
686 1.1 ad
687 1.1 ad void
688 1.1 ad icp_ccb_unmap(struct icp_softc *icp, struct icp_ccb *ic)
689 1.1 ad {
690 1.1 ad int i;
691 1.1 ad
692 1.1 ad if ((ic->ic_flags & IC_XFER_OUT) != 0)
693 1.1 ad i = BUS_DMASYNC_POSTWRITE;
694 1.1 ad else /* if ((ic->ic_flags & IC_XFER_IN) != 0) */
695 1.1 ad i = BUS_DMASYNC_POSTREAD;
696 1.1 ad
697 1.1 ad bus_dmamap_sync(icp->icp_dmat, ic->ic_xfer_map, 0, ic->ic_xfer_size, i);
698 1.1 ad bus_dmamap_unload(icp->icp_dmat, ic->ic_xfer_map);
699 1.1 ad }
700 1.1 ad
701 1.1 ad int
702 1.1 ad icp_ccb_poll(struct icp_softc *icp, struct icp_ccb *ic, int timo)
703 1.1 ad {
704 1.1 ad int rv;
705 1.1 ad
706 1.1 ad for (timo = ICP_BUSY_WAIT_MS * 100; timo != 0; timo--) {
707 1.1 ad if (!(*icp->icp_test_busy)(icp))
708 1.1 ad break;
709 1.1 ad DELAY(10);
710 1.1 ad }
711 1.1 ad if (timo == 0) {
712 1.1 ad printf("%s: submit: busy\n", icp->icp_dv.dv_xname);
713 1.1 ad return (EAGAIN);
714 1.1 ad }
715 1.1 ad
716 1.1 ad icp_ccb_submit(icp, ic);
717 1.1 ad
718 1.1 ad for (timo *= 10; timo != 0; timo--) {
719 1.1 ad DELAY(100);
720 1.1 ad icp_intr(icp);
721 1.1 ad if ((ic->ic_flags & IC_COMPLETE) != 0)
722 1.1 ad break;
723 1.1 ad }
724 1.1 ad
725 1.1 ad if (timo != 0) {
726 1.1 ad if (ic->ic_status != ICP_S_OK) {
727 1.1 ad #ifdef ICP_DEBUG
728 1.1 ad printf("%s: request failed; status=0x%04x\n",
729 1.1 ad icp->icp_dv.dv_xname, ic->ic_status);
730 1.1 ad #endif
731 1.1 ad rv = EIO;
732 1.1 ad } else
733 1.1 ad rv = 0;
734 1.1 ad } else {
735 1.1 ad printf("%s: command timed out\n", icp->icp_dv.dv_xname);
736 1.1 ad rv = EIO;
737 1.1 ad }
738 1.1 ad
739 1.1 ad while ((*icp->icp_test_busy)(icp) != 0)
740 1.1 ad DELAY(10);
741 1.1 ad
742 1.1 ad return (rv);
743 1.1 ad }
744 1.1 ad
745 1.1 ad int
746 1.1 ad icp_ccb_wait(struct icp_softc *icp, struct icp_ccb *ic, int timo)
747 1.1 ad {
748 1.1 ad int s, rv;
749 1.1 ad
750 1.1 ad ic->ic_flags |= IC_WAITING;
751 1.1 ad
752 1.1 ad s = splbio();
753 1.1 ad icp_ccb_enqueue(icp, ic);
754 1.1 ad if ((rv = tsleep(ic, PRIBIO, "icpwccb", mstohz(timo))) != 0) {
755 1.1 ad splx(s);
756 1.1 ad return (rv);
757 1.1 ad }
758 1.1 ad splx(s);
759 1.1 ad
760 1.1 ad if (ic->ic_status != ICP_S_OK) {
761 1.1 ad printf("%s: command failed; status=%x\n", icp->icp_dv.dv_xname,
762 1.1 ad ic->ic_status);
763 1.1 ad return (EIO);
764 1.1 ad }
765 1.1 ad
766 1.1 ad return (0);
767 1.1 ad }
768 1.1 ad
769 1.1 ad void
770 1.1 ad icp_ccb_submit(struct icp_softc *icp, struct icp_ccb *ic)
771 1.1 ad {
772 1.1 ad
773 1.1 ad ic->ic_cmdlen = (ic->ic_cmdlen + 3) & ~3;
774 1.1 ad
775 1.1 ad (*icp->icp_set_sema0)(icp);
776 1.1 ad DELAY(10);
777 1.1 ad
778 1.1 ad ic->ic_cmd.cmd_boardnode = htole32(ICP_LOCALBOARD);
779 1.1 ad ic->ic_cmd.cmd_cmdindex = htole32(ic->ic_ident);
780 1.1 ad
781 1.1 ad (*icp->icp_copy_cmd)(icp, ic);
782 1.1 ad (*icp->icp_release_event)(icp, ic);
783 1.1 ad }
784