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