Rename sprintf -> ksprintf
[dfdiff.git] / sys / dev / raid / iir / iir.c
blobfd3ac803facc3a0dfe8bc2edc90154914f86b8b6
1 /* $FreeBSD: src/sys/dev/iir/iir.c,v 1.2.2.3 2002/05/05 08:18:12 asmodai Exp $ */
2 /* $DragonFly: src/sys/dev/raid/iir/iir.c,v 1.15 2006/12/20 18:14:40 dillon Exp $ */
3 /*
4 * Copyright (c) 2000-01 Intel Corporation
5 * All Rights Reserved
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * iir.c: SCSI dependant code for the Intel Integrated RAID Controller driver
35 * Written by: Achim Leubner <achim.leubner@intel.com>
36 * Fixes/Additions: Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>
38 * credits: Niklas Hallqvist; OpenBSD driver for the ICP Controllers.
39 * Mike Smith; Some driver source code.
40 * FreeBSD.ORG; Great O/S to work on and for.
42 * TODO:
45 #ident "$Id: iir.c 1.2 2001/06/21 20:28:32 achim Exp $"
47 #define _IIR_C_
49 /* #include "opt_iir.h" */
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/types.h>
53 #include <sys/eventhandler.h>
54 #include <sys/malloc.h>
55 #include <sys/kernel.h>
56 #include <sys/bus.h>
57 #include <sys/thread2.h>
59 #include <machine/clock.h>
60 #include <machine/stdarg.h>
62 #include <bus/cam/cam.h>
63 #include <bus/cam/cam_ccb.h>
64 #include <bus/cam/cam_sim.h>
65 #include <bus/cam/cam_xpt_sim.h>
66 #include <bus/cam/cam_debug.h>
67 #include <bus/cam/scsi/scsi_all.h>
68 #include <bus/cam/scsi/scsi_message.h>
70 #include <vm/vm.h>
71 #include <vm/pmap.h>
73 #include "iir.h"
75 struct gdt_softc *gdt_wait_gdt;
76 int gdt_wait_index;
78 #ifdef GDT_DEBUG
79 int gdt_debug = GDT_DEBUG;
80 #ifdef __SERIAL__
81 #define MAX_SERBUF 160
82 static void ser_init(void);
83 static void ser_puts(char *str);
84 static void ser_putc(int c);
85 static char strbuf[MAX_SERBUF+1];
86 #ifdef __COM2__
87 #define COM_BASE 0x2f8
88 #else
89 #define COM_BASE 0x3f8
90 #endif
91 static void ser_init()
93 unsigned port=COM_BASE;
95 outb(port+3, 0x80);
96 outb(port+1, 0);
97 /* 19200 Baud, if 9600: outb(12,port) */
98 outb(port, 6);
99 outb(port+3, 3);
100 outb(port+1, 0);
103 static void ser_puts(char *str)
105 char *ptr;
107 ser_init();
108 for (ptr=str;*ptr;++ptr)
109 ser_putc((int)(*ptr));
112 static void ser_putc(int c)
114 unsigned port=COM_BASE;
116 while ((inb(port+5) & 0x20)==0);
117 outb(port, c);
118 if (c==0x0a)
120 while ((inb(port+5) & 0x20)==0);
121 outb(port, 0x0d);
126 ser_printf(const char *fmt, ...)
128 __va_list args;
129 int i;
131 __va_start(args,fmt);
132 i = kvsprintf(strbuf,fmt,args);
133 ser_puts(strbuf);
134 __va_end(args);
135 return i;
137 #endif
138 #endif
140 /* The linked list of softc structures */
141 struct gdt_softc_list gdt_softcs = TAILQ_HEAD_INITIALIZER(gdt_softcs);
142 /* controller cnt. */
143 int gdt_cnt = 0;
144 /* event buffer */
145 static gdt_evt_str ebuffer[GDT_MAX_EVENTS];
146 static int elastidx, eoldidx;
147 /* statistics */
148 gdt_statist_t gdt_stat;
150 /* Definitions for our use of the SIM private CCB area */
151 #define ccb_sim_ptr spriv_ptr0
152 #define ccb_priority spriv_field1
154 static void iir_action(struct cam_sim *sim, union ccb *ccb);
155 static void iir_poll(struct cam_sim *sim);
156 static void iir_shutdown(void *arg, int howto);
157 static void iir_timeout(void *arg);
158 static void iir_watchdog(void *arg);
160 static void gdt_eval_mapping(u_int32_t size, int *cyls, int *heads,
161 int *secs);
162 static int gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
163 u_int8_t service, u_int16_t opcode,
164 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3);
165 static int gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *ccb,
166 int timeout);
168 static struct gdt_ccb *gdt_get_ccb(struct gdt_softc *gdt);
169 static u_int32_t gdt_ccb_vtop(struct gdt_softc *gdt,
170 struct gdt_ccb *gccb);
172 static int gdt_sync_event(struct gdt_softc *gdt, int service,
173 u_int8_t index, struct gdt_ccb *gccb);
174 static int gdt_async_event(struct gdt_softc *gdt, int service);
175 static struct gdt_ccb *gdt_raw_cmd(struct gdt_softc *gdt, union ccb *ccb);
176 static struct gdt_ccb *gdt_cache_cmd(struct gdt_softc *gdt, union ccb *ccb);
177 static struct gdt_ccb *gdt_ioctl_cmd(struct gdt_softc *gdt, gdt_ucmd_t *ucmd);
178 static void gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb);
180 static void gdtmapmem(void *arg, bus_dma_segment_t *dm_segs,
181 int nseg, int error);
182 static void gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
183 int nseg, int error);
186 iir_init(struct gdt_softc *gdt)
188 u_int16_t cdev_cnt;
189 int i, id, drv_cyls, drv_hds, drv_secs;
190 struct gdt_ccb *gccb;
192 GDT_DPRINTF(GDT_D_DEBUG, ("iir_init()\n"));
194 gdt->sc_state = GDT_POLLING;
195 gdt_clear_events();
196 bzero(&gdt_stat, sizeof(gdt_statist_t));
198 SLIST_INIT(&gdt->sc_free_gccb);
199 SLIST_INIT(&gdt->sc_pending_gccb);
200 TAILQ_INIT(&gdt->sc_ccb_queue);
201 TAILQ_INIT(&gdt->sc_ucmd_queue);
202 TAILQ_INSERT_TAIL(&gdt_softcs, gdt, links);
204 /* DMA tag for mapping buffers into device visible space. */
205 if (bus_dma_tag_create(gdt->sc_parent_dmat, /*alignment*/1, /*boundary*/0,
206 /*lowaddr*/BUS_SPACE_MAXADDR,
207 /*highaddr*/BUS_SPACE_MAXADDR,
208 /*filter*/NULL, /*filterarg*/NULL,
209 /*maxsize*/MAXBSIZE, /*nsegments*/GDT_MAXSG,
210 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
211 /*flags*/BUS_DMA_ALLOCNOW,
212 &gdt->sc_buffer_dmat) != 0) {
213 printf("iir%d: bus_dma_tag_create(...,gdt->sc_buffer_dmat) failed\n",
214 gdt->sc_hanum);
215 return (1);
217 gdt->sc_init_level++;
219 /* DMA tag for our ccb structures */
220 if (bus_dma_tag_create(gdt->sc_parent_dmat, /*alignment*/1, /*boundary*/0,
221 /*lowaddr*/BUS_SPACE_MAXADDR,
222 /*highaddr*/BUS_SPACE_MAXADDR,
223 /*filter*/NULL, /*filterarg*/NULL,
224 GDT_MAXCMDS * sizeof(struct gdt_ccb),
225 /*nsegments*/1,
226 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
227 /*flags*/0, &gdt->sc_gccb_dmat) != 0) {
228 printf("iir%d: bus_dma_tag_create(...,gdt->sc_gccb_dmat) failed\n",
229 gdt->sc_hanum);
230 return (1);
232 gdt->sc_init_level++;
234 /* Allocation for our ccbs */
235 if (bus_dmamem_alloc(gdt->sc_gccb_dmat, (void **)&gdt->sc_gccbs,
236 BUS_DMA_NOWAIT, &gdt->sc_gccb_dmamap) != 0) {
237 printf("iir%d: bus_dmamem_alloc(...,&gdt->sc_gccbs,...) failed\n",
238 gdt->sc_hanum);
239 return (1);
241 gdt->sc_init_level++;
243 /* And permanently map them */
244 bus_dmamap_load(gdt->sc_gccb_dmat, gdt->sc_gccb_dmamap,
245 gdt->sc_gccbs, GDT_MAXCMDS * sizeof(struct gdt_ccb),
246 gdtmapmem, &gdt->sc_gccb_busbase, /*flags*/0);
247 gdt->sc_init_level++;
249 /* Clear them out. */
250 bzero(gdt->sc_gccbs, GDT_MAXCMDS * sizeof(struct gdt_ccb));
252 /* Initialize the ccbs */
253 for (i = GDT_MAXCMDS-1; i >= 0; i--) {
254 gdt->sc_gccbs[i].gc_cmd_index = i + 2;
255 gdt->sc_gccbs[i].gc_flags = GDT_GCF_UNUSED;
256 gdt->sc_gccbs[i].gc_map_flag = FALSE;
257 if (bus_dmamap_create(gdt->sc_buffer_dmat, /*flags*/0,
258 &gdt->sc_gccbs[i].gc_dmamap) != 0)
259 return(1);
260 gdt->sc_gccbs[i].gc_map_flag = TRUE;
261 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, &gdt->sc_gccbs[i], sle);
263 gdt->sc_init_level++;
265 /* create the control device */
266 gdt->sc_dev = gdt_make_dev(gdt->sc_hanum);
268 /* allocate ccb for gdt_internal_cmd() */
269 gccb = gdt_get_ccb(gdt);
270 if (gccb == NULL) {
271 printf("iir%d: No free command index found\n",
272 gdt->sc_hanum);
273 return (1);
276 if (!gdt_internal_cmd(gdt, gccb, GDT_SCREENSERVICE, GDT_INIT,
277 0, 0, 0)) {
278 printf("iir%d: Screen service initialization error %d\n",
279 gdt->sc_hanum, gdt->sc_status);
280 gdt_free_ccb(gdt, gccb);
281 return (1);
284 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INIT,
285 GDT_LINUX_OS, 0, 0)) {
286 printf("iir%d: Cache service initialization error %d\n",
287 gdt->sc_hanum, gdt->sc_status);
288 gdt_free_ccb(gdt, gccb);
289 return (1);
291 gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_UNFREEZE_IO,
292 0, 0, 0);
294 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_MOUNT,
295 0xffff, 1, 0)) {
296 printf("iir%d: Cache service mount error %d\n",
297 gdt->sc_hanum, gdt->sc_status);
298 gdt_free_ccb(gdt, gccb);
299 return (1);
302 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INIT,
303 GDT_LINUX_OS, 0, 0)) {
304 printf("iir%d: Cache service post-mount initialization error %d\n",
305 gdt->sc_hanum, gdt->sc_status);
306 gdt_free_ccb(gdt, gccb);
307 return (1);
309 cdev_cnt = (u_int16_t)gdt->sc_info;
310 gdt->sc_fw_vers = gdt->sc_service;
312 /* Detect number of buses */
313 gdt_enc32(gccb->gc_scratch + GDT_IOC_VERSION, GDT_IOC_NEWEST);
314 gccb->gc_scratch[GDT_IOC_LIST_ENTRIES] = GDT_MAXBUS;
315 gccb->gc_scratch[GDT_IOC_FIRST_CHAN] = 0;
316 gccb->gc_scratch[GDT_IOC_LAST_CHAN] = GDT_MAXBUS - 1;
317 gdt_enc32(gccb->gc_scratch + GDT_IOC_LIST_OFFSET, GDT_IOC_HDR_SZ);
318 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
319 GDT_IOCHAN_RAW_DESC, GDT_INVALID_CHANNEL,
320 GDT_IOC_HDR_SZ + GDT_MAXBUS * GDT_RAWIOC_SZ)) {
321 gdt->sc_bus_cnt = gccb->gc_scratch[GDT_IOC_CHAN_COUNT];
322 for (i = 0; i < gdt->sc_bus_cnt; i++) {
323 id = gccb->gc_scratch[GDT_IOC_HDR_SZ +
324 i * GDT_RAWIOC_SZ + GDT_RAWIOC_PROC_ID];
325 gdt->sc_bus_id[i] = id < GDT_MAXID_FC ? id : 0xff;
327 } else {
328 /* New method failed, use fallback. */
329 for (i = 0; i < GDT_MAXBUS; i++) {
330 gdt_enc32(gccb->gc_scratch + GDT_GETCH_CHANNEL_NO, i);
331 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
332 GDT_SCSI_CHAN_CNT | GDT_L_CTRL_PATTERN,
333 GDT_IO_CHANNEL | GDT_INVALID_CHANNEL,
334 GDT_GETCH_SZ)) {
335 if (i == 0) {
336 printf("iir%d: Cannot get channel count, "
337 "error %d\n", gdt->sc_hanum, gdt->sc_status);
338 gdt_free_ccb(gdt, gccb);
339 return (1);
341 break;
343 gdt->sc_bus_id[i] =
344 (gccb->gc_scratch[GDT_GETCH_SIOP_ID] < GDT_MAXID_FC) ?
345 gccb->gc_scratch[GDT_GETCH_SIOP_ID] : 0xff;
347 gdt->sc_bus_cnt = i;
349 /* add one "virtual" channel for the host drives */
350 gdt->sc_virt_bus = gdt->sc_bus_cnt;
351 gdt->sc_bus_cnt++;
353 if (!gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_INIT,
354 0, 0, 0)) {
355 printf("iir%d: Raw service initialization error %d\n",
356 gdt->sc_hanum, gdt->sc_status);
357 gdt_free_ccb(gdt, gccb);
358 return (1);
361 /* Set/get features raw service (scatter/gather) */
362 gdt->sc_raw_feat = 0;
363 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_SET_FEAT,
364 GDT_SCATTER_GATHER, 0, 0)) {
365 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_GET_FEAT,
366 0, 0, 0)) {
367 gdt->sc_raw_feat = gdt->sc_info;
368 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
369 panic("iir%d: Scatter/Gather Raw Service "
370 "required but not supported!\n", gdt->sc_hanum);
371 gdt_free_ccb(gdt, gccb);
372 return (1);
377 /* Set/get features cache service (scatter/gather) */
378 gdt->sc_cache_feat = 0;
379 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_SET_FEAT,
380 0, GDT_SCATTER_GATHER, 0)) {
381 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_GET_FEAT,
382 0, 0, 0)) {
383 gdt->sc_cache_feat = gdt->sc_info;
384 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
385 panic("iir%d: Scatter/Gather Cache Service "
386 "required but not supported!\n", gdt->sc_hanum);
387 gdt_free_ccb(gdt, gccb);
388 return (1);
393 /* Scan for cache devices */
394 for (i = 0; i < cdev_cnt && i < GDT_MAX_HDRIVES; i++) {
395 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INFO,
396 i, 0, 0)) {
397 gdt->sc_hdr[i].hd_present = 1;
398 gdt->sc_hdr[i].hd_size = gdt->sc_info;
401 * Evaluate mapping (sectors per head, heads per cyl)
403 gdt->sc_hdr[i].hd_size &= ~GDT_SECS32;
404 if (gdt->sc_info2 == 0)
405 gdt_eval_mapping(gdt->sc_hdr[i].hd_size,
406 &drv_cyls, &drv_hds, &drv_secs);
407 else {
408 drv_hds = gdt->sc_info2 & 0xff;
409 drv_secs = (gdt->sc_info2 >> 8) & 0xff;
410 drv_cyls = gdt->sc_hdr[i].hd_size / drv_hds /
411 drv_secs;
413 gdt->sc_hdr[i].hd_heads = drv_hds;
414 gdt->sc_hdr[i].hd_secs = drv_secs;
415 /* Round the size */
416 gdt->sc_hdr[i].hd_size = drv_cyls * drv_hds * drv_secs;
418 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE,
419 GDT_DEVTYPE, i, 0, 0))
420 gdt->sc_hdr[i].hd_devtype = gdt->sc_info;
424 GDT_DPRINTF(GDT_D_INIT, ("dpmem %x %d-bus %d cache device%s\n",
425 gdt->sc_dpmembase,
426 gdt->sc_bus_cnt, cdev_cnt,
427 cdev_cnt == 1 ? "" : "s"));
428 gdt_free_ccb(gdt, gccb);
430 gdt_cnt++;
431 return (0);
434 void
435 iir_free(struct gdt_softc *gdt)
437 int i;
439 GDT_DPRINTF(GDT_D_INIT, ("iir_free()\n"));
441 switch (gdt->sc_init_level) {
442 default:
443 gdt_destroy_dev(gdt->sc_dev);
444 case 5:
445 for (i = GDT_MAXCMDS-1; i >= 0; i--)
446 if (gdt->sc_gccbs[i].gc_map_flag)
447 bus_dmamap_destroy(gdt->sc_buffer_dmat,
448 gdt->sc_gccbs[i].gc_dmamap);
449 bus_dmamap_unload(gdt->sc_gccb_dmat, gdt->sc_gccb_dmamap);
450 case 4:
451 bus_dmamem_free(gdt->sc_gccb_dmat, gdt->sc_gccbs, gdt->sc_gccb_dmamap);
452 case 3:
453 bus_dma_tag_destroy(gdt->sc_gccb_dmat);
454 case 2:
455 bus_dma_tag_destroy(gdt->sc_buffer_dmat);
456 case 1:
457 bus_dma_tag_destroy(gdt->sc_parent_dmat);
458 case 0:
459 break;
461 TAILQ_REMOVE(&gdt_softcs, gdt, links);
464 void
465 iir_attach(struct gdt_softc *gdt)
467 struct cam_devq *devq;
468 int i;
470 GDT_DPRINTF(GDT_D_INIT, ("iir_attach()\n"));
472 callout_init(&gdt->watchdog_timer);
474 * Create the device queue for our SIM.
476 devq = cam_simq_alloc(GDT_MAXCMDS);
477 if (devq == NULL)
478 return;
480 for (i = 0; i < gdt->sc_bus_cnt; i++) {
482 * Construct our SIM entry
484 gdt->sims[i] = cam_sim_alloc(iir_action, iir_poll, "iir",
485 gdt, gdt->sc_hanum, /*untagged*/2,
486 /*tagged*/GDT_MAXCMDS, devq);
487 if (xpt_bus_register(gdt->sims[i], i) != CAM_SUCCESS) {
488 cam_sim_free(gdt->sims[i]);
489 break;
492 if (xpt_create_path(&gdt->paths[i], /*periph*/NULL,
493 cam_sim_path(gdt->sims[i]),
494 CAM_TARGET_WILDCARD,
495 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
496 xpt_bus_deregister(cam_sim_path(gdt->sims[i]));
497 cam_sim_free(gdt->sims[i]);
498 break;
501 cam_simq_release(devq);
502 if (i > 0)
503 EVENTHANDLER_REGISTER(shutdown_final, iir_shutdown,
504 gdt, SHUTDOWN_PRI_DEFAULT);
505 /* iir_watchdog(gdt); */
506 gdt->sc_state = GDT_NORMAL;
509 static void
510 gdt_eval_mapping(u_int32_t size, int *cyls, int *heads, int *secs)
512 *cyls = size / GDT_HEADS / GDT_SECS;
513 if (*cyls < GDT_MAXCYLS) {
514 *heads = GDT_HEADS;
515 *secs = GDT_SECS;
516 } else {
517 /* Too high for 64 * 32 */
518 *cyls = size / GDT_MEDHEADS / GDT_MEDSECS;
519 if (*cyls < GDT_MAXCYLS) {
520 *heads = GDT_MEDHEADS;
521 *secs = GDT_MEDSECS;
522 } else {
523 /* Too high for 127 * 63 */
524 *cyls = size / GDT_BIGHEADS / GDT_BIGSECS;
525 *heads = GDT_BIGHEADS;
526 *secs = GDT_BIGSECS;
531 static int
532 gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *gccb,
533 int timeout)
535 int rv = 0;
537 GDT_DPRINTF(GDT_D_INIT,
538 ("gdt_wait(%p, %p, %d)\n", gdt, gccb, timeout));
540 gdt->sc_state |= GDT_POLL_WAIT;
541 do {
542 iir_intr(gdt);
543 if (gdt == gdt_wait_gdt &&
544 gccb->gc_cmd_index == gdt_wait_index) {
545 rv = 1;
546 break;
548 DELAY(1);
549 } while (--timeout);
550 gdt->sc_state &= ~GDT_POLL_WAIT;
552 while (gdt->sc_test_busy(gdt))
553 DELAY(1); /* XXX correct? */
555 return (rv);
558 static int
559 gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
560 u_int8_t service, u_int16_t opcode,
561 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
563 int retries;
565 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cmd(%p, %d, %d, %d, %d, %d)\n",
566 gdt, service, opcode, arg1, arg2, arg3));
568 bzero(gdt->sc_cmd, GDT_CMD_SZ);
570 for (retries = GDT_RETRIES; ; ) {
571 gccb->gc_service = service;
572 gccb->gc_flags = GDT_GCF_INTERNAL;
574 gdt->sc_set_sema0(gdt);
575 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
576 gccb->gc_cmd_index);
577 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, opcode);
579 switch (service) {
580 case GDT_CACHESERVICE:
581 if (opcode == GDT_IOCTL) {
582 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
583 GDT_IOCTL_SUBFUNC, arg1);
584 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
585 GDT_IOCTL_CHANNEL, arg2);
586 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION +
587 GDT_IOCTL_PARAM_SIZE, (u_int16_t)arg3);
588 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
589 gdt_ccb_vtop(gdt, gccb) +
590 offsetof(struct gdt_ccb, gc_scratch[0]));
591 } else {
592 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION +
593 GDT_CACHE_DEVICENO, (u_int16_t)arg1);
594 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
595 GDT_CACHE_BLOCKNO, arg2);
597 break;
599 case GDT_SCSIRAWSERVICE:
600 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
601 GDT_RAW_DIRECTION, arg1);
602 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
603 (u_int8_t)arg2;
604 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
605 (u_int8_t)arg3;
606 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
607 (u_int8_t)(arg3 >> 8);
610 gdt->sc_cmd_len = GDT_CMD_SZ;
611 gdt->sc_cmd_off = 0;
612 gdt->sc_cmd_cnt = 0;
613 gdt->sc_copy_cmd(gdt, gccb);
614 gdt->sc_release_event(gdt);
615 DELAY(20);
616 if (!gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT))
617 return (0);
618 if (gdt->sc_status != GDT_S_BSY || --retries == 0)
619 break;
620 DELAY(1);
622 return (gdt->sc_status == GDT_S_OK);
625 static struct gdt_ccb *
626 gdt_get_ccb(struct gdt_softc *gdt)
628 struct gdt_ccb *gccb;
630 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_get_ccb(%p)\n", gdt));
632 crit_enter();
633 gccb = SLIST_FIRST(&gdt->sc_free_gccb);
634 if (gccb != NULL) {
635 SLIST_REMOVE_HEAD(&gdt->sc_free_gccb, sle);
636 SLIST_INSERT_HEAD(&gdt->sc_pending_gccb, gccb, sle);
637 ++gdt_stat.cmd_index_act;
638 if (gdt_stat.cmd_index_act > gdt_stat.cmd_index_max)
639 gdt_stat.cmd_index_max = gdt_stat.cmd_index_act;
641 crit_exit();
642 return (gccb);
645 void
646 gdt_free_ccb(struct gdt_softc *gdt, struct gdt_ccb *gccb)
648 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_free_ccb(%p, %p)\n", gdt, gccb));
650 crit_enter();
651 gccb->gc_flags = GDT_GCF_UNUSED;
652 SLIST_REMOVE(&gdt->sc_pending_gccb, gccb, gdt_ccb, sle);
653 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
654 --gdt_stat.cmd_index_act;
655 crit_exit();
656 if (gdt->sc_state & GDT_SHUTDOWN)
657 wakeup(gccb);
660 static u_int32_t
661 gdt_ccb_vtop(struct gdt_softc *gdt, struct gdt_ccb *gccb)
663 return (gdt->sc_gccb_busbase
664 + (u_int32_t)((caddr_t)gccb - (caddr_t)gdt->sc_gccbs));
667 void
668 gdt_next(struct gdt_softc *gdt)
670 union ccb *ccb;
671 gdt_ucmd_t *ucmd;
672 struct cam_sim *sim;
673 int bus, target, lun;
674 int next_cmd;
676 struct ccb_scsiio *csio;
677 struct ccb_hdr *ccbh;
678 struct gdt_ccb *gccb = NULL;
679 u_int8_t cmd;
681 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_next(%p)\n", gdt));
683 crit_enter();
684 if (gdt->sc_test_busy(gdt)) {
685 if (!(gdt->sc_state & GDT_POLLING)) {
686 crit_exit();
687 return;
689 while (gdt->sc_test_busy(gdt))
690 DELAY(1);
693 gdt->sc_cmd_cnt = gdt->sc_cmd_off = 0;
694 next_cmd = TRUE;
695 for (;;) {
696 /* I/Os in queue? controller ready? */
697 if (!TAILQ_FIRST(&gdt->sc_ucmd_queue) &&
698 !TAILQ_FIRST(&gdt->sc_ccb_queue))
699 break;
701 /* 1.: I/Os without ccb (IOCTLs) */
702 ucmd = TAILQ_FIRST(&gdt->sc_ucmd_queue);
703 if (ucmd != NULL) {
704 TAILQ_REMOVE(&gdt->sc_ucmd_queue, ucmd, links);
705 if ((gccb = gdt_ioctl_cmd(gdt, ucmd)) == NULL) {
706 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
707 break;
709 break;
710 /* wenn mehrere Kdos. zulassen: if (!gdt_polling) continue; */
713 /* 2.: I/Os with ccb */
714 ccb = (union ccb *)TAILQ_FIRST(&gdt->sc_ccb_queue);
715 /* ist dann immer != NULL, da oben getestet */
716 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
717 bus = cam_sim_bus(sim);
718 target = ccb->ccb_h.target_id;
719 lun = ccb->ccb_h.target_lun;
721 TAILQ_REMOVE(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
722 --gdt_stat.req_queue_act;
723 /* ccb->ccb_h.func_code is XPT_SCSI_IO */
724 GDT_DPRINTF(GDT_D_QUEUE, ("XPT_SCSI_IO flags 0x%x)\n",
725 ccb->ccb_h.flags));
726 csio = &ccb->csio;
727 ccbh = &ccb->ccb_h;
728 cmd = csio->cdb_io.cdb_bytes[0];
729 /* Max CDB length is 12 bytes */
730 if (csio->cdb_len > 12) {
731 ccbh->status = CAM_REQ_INVALID;
732 --gdt_stat.io_count_act;
733 xpt_done(ccb);
734 } else if (bus != gdt->sc_virt_bus) {
735 /* raw service command */
736 if ((gccb = gdt_raw_cmd(gdt, ccb)) == NULL) {
737 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
738 sim_links.tqe);
739 ++gdt_stat.req_queue_act;
740 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
741 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
742 next_cmd = FALSE;
744 } else if (target >= GDT_MAX_HDRIVES ||
745 !gdt->sc_hdr[target].hd_present || lun != 0) {
746 ccbh->status = CAM_SEL_TIMEOUT;
747 --gdt_stat.io_count_act;
748 xpt_done(ccb);
749 } else {
750 /* cache service command */
751 if (cmd == READ_6 || cmd == WRITE_6 ||
752 cmd == READ_10 || cmd == WRITE_10) {
753 if ((gccb = gdt_cache_cmd(gdt, ccb)) == NULL) {
754 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
755 sim_links.tqe);
756 ++gdt_stat.req_queue_act;
757 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
758 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
759 next_cmd = FALSE;
761 } else {
762 crit_exit();
763 gdt_internal_cache_cmd(gdt, ccb);
764 crit_enter();
767 if ((gdt->sc_state & GDT_POLLING) || !next_cmd)
768 break;
770 if (gdt->sc_cmd_cnt > 0)
771 gdt->sc_release_event(gdt);
773 crit_exit();
775 if ((gdt->sc_state & GDT_POLLING) && gdt->sc_cmd_cnt > 0) {
776 gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT);
780 static struct gdt_ccb *
781 gdt_raw_cmd(struct gdt_softc *gdt, union ccb *ccb)
783 struct gdt_ccb *gccb;
784 struct cam_sim *sim;
786 GDT_DPRINTF(GDT_D_CMD, ("gdt_raw_cmd(%p, %p)\n", gdt, ccb));
788 if (roundup(GDT_CMD_UNION + GDT_RAW_SZ, sizeof(u_int32_t)) +
789 gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
790 gdt->sc_ic_all_size) {
791 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_raw_cmd(): DPMEM overflow\n",
792 gdt->sc_hanum));
793 return (NULL);
796 bzero(gdt->sc_cmd, GDT_CMD_SZ);
798 gccb = gdt_get_ccb(gdt);
799 if (gccb == NULL) {
800 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: No free command index found\n",
801 gdt->sc_hanum));
802 return (gccb);
804 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
805 gccb->gc_ccb = ccb;
806 gccb->gc_service = GDT_SCSIRAWSERVICE;
807 gccb->gc_flags = GDT_GCF_SCSI;
809 if (gdt->sc_cmd_cnt == 0)
810 gdt->sc_set_sema0(gdt);
811 crit_exit();
812 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
813 gccb->gc_cmd_index);
814 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
816 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
817 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
818 GDT_DATA_IN : GDT_DATA_OUT);
819 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
820 ccb->csio.dxfer_len);
821 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
822 ccb->csio.cdb_len);
823 bcopy(ccb->csio.cdb_io.cdb_bytes, gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
824 ccb->csio.cdb_len);
825 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
826 ccb->ccb_h.target_id;
827 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
828 ccb->ccb_h.target_lun;
829 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
830 cam_sim_bus(sim);
831 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
832 sizeof(struct scsi_sense_data));
833 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
834 gdt_ccb_vtop(gdt, gccb) +
835 offsetof(struct gdt_ccb, gc_scratch[0]));
838 * If we have any data to send with this command,
839 * map it into bus space.
841 /* Only use S/G if there is a transfer */
842 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
843 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
844 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
845 int error;
847 /* vorher unlock von splcam() ??? */
848 crit_enter();
849 error =
850 bus_dmamap_load(gdt->sc_buffer_dmat,
851 gccb->gc_dmamap,
852 ccb->csio.data_ptr,
853 ccb->csio.dxfer_len,
854 gdtexecuteccb,
855 gccb, /*flags*/0);
856 if (error == EINPROGRESS) {
857 xpt_freeze_simq(sim, 1);
858 gccb->gc_state |= CAM_RELEASE_SIMQ;
860 crit_exit();
861 } else {
862 struct bus_dma_segment seg;
864 /* Pointer to physical buffer */
865 seg.ds_addr =
866 (bus_addr_t)ccb->csio.data_ptr;
867 seg.ds_len = ccb->csio.dxfer_len;
868 gdtexecuteccb(gccb, &seg, 1, 0);
870 } else {
871 struct bus_dma_segment *segs;
873 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
874 panic("iir%d: iir_action - Physical "
875 "segment pointers unsupported", gdt->sc_hanum);
877 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
878 panic("iir%d: iir_action - Virtual "
879 "segment addresses unsupported", gdt->sc_hanum);
881 /* Just use the segments provided */
882 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
883 gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
885 } else {
886 gdtexecuteccb(gccb, NULL, 0, 0);
889 crit_enter();
890 return (gccb);
893 static struct gdt_ccb *
894 gdt_cache_cmd(struct gdt_softc *gdt, union ccb *ccb)
896 struct gdt_ccb *gccb;
897 struct cam_sim *sim;
898 u_int8_t *cmdp;
899 u_int16_t opcode;
900 u_int32_t blockno, blockcnt;
902 GDT_DPRINTF(GDT_D_CMD, ("gdt_cache_cmd(%p, %p)\n", gdt, ccb));
904 if (roundup(GDT_CMD_UNION + GDT_CACHE_SZ, sizeof(u_int32_t)) +
905 gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
906 gdt->sc_ic_all_size) {
907 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_cache_cmd(): DPMEM overflow\n",
908 gdt->sc_hanum));
909 return (NULL);
912 bzero(gdt->sc_cmd, GDT_CMD_SZ);
914 gccb = gdt_get_ccb(gdt);
915 if (gccb == NULL) {
916 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
917 gdt->sc_hanum));
918 return (gccb);
920 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
921 gccb->gc_ccb = ccb;
922 gccb->gc_service = GDT_CACHESERVICE;
923 gccb->gc_flags = GDT_GCF_SCSI;
925 if (gdt->sc_cmd_cnt == 0)
926 gdt->sc_set_sema0(gdt);
927 crit_exit();
928 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
929 gccb->gc_cmd_index);
930 cmdp = ccb->csio.cdb_io.cdb_bytes;
931 opcode = (*cmdp == WRITE_6 || *cmdp == WRITE_10) ? GDT_WRITE : GDT_READ;
932 if ((gdt->sc_state & GDT_SHUTDOWN) && opcode == GDT_WRITE)
933 opcode = GDT_WRITE_THR;
934 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, opcode);
936 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
937 ccb->ccb_h.target_id);
938 if (ccb->csio.cdb_len == 6) {
939 struct scsi_rw_6 *rw = (struct scsi_rw_6 *)cmdp;
940 blockno = scsi_3btoul(rw->addr) & ((SRW_TOPADDR<<16) | 0xffff);
941 blockcnt = rw->length ? rw->length : 0x100;
942 } else {
943 struct scsi_rw_10 *rw = (struct scsi_rw_10 *)cmdp;
944 blockno = scsi_4btoul(rw->addr);
945 blockcnt = scsi_2btoul(rw->length);
947 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
948 blockno);
949 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
950 blockcnt);
953 * If we have any data to send with this command,
954 * map it into bus space.
956 /* Only use S/G if there is a transfer */
957 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
958 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
959 int error;
961 /* vorher unlock von splcam() ??? */
962 crit_enter();
963 error =
964 bus_dmamap_load(gdt->sc_buffer_dmat,
965 gccb->gc_dmamap,
966 ccb->csio.data_ptr,
967 ccb->csio.dxfer_len,
968 gdtexecuteccb,
969 gccb, /*flags*/0);
970 if (error == EINPROGRESS) {
971 xpt_freeze_simq(sim, 1);
972 gccb->gc_state |= CAM_RELEASE_SIMQ;
974 crit_exit();
975 } else {
976 struct bus_dma_segment seg;
978 /* Pointer to physical buffer */
979 seg.ds_addr =
980 (bus_addr_t)ccb->csio.data_ptr;
981 seg.ds_len = ccb->csio.dxfer_len;
982 gdtexecuteccb(gccb, &seg, 1, 0);
984 } else {
985 struct bus_dma_segment *segs;
987 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
988 panic("iir%d: iir_action - Physical "
989 "segment pointers unsupported", gdt->sc_hanum);
991 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
992 panic("iir%d: iir_action - Virtual "
993 "segment addresses unsupported", gdt->sc_hanum);
995 /* Just use the segments provided */
996 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
997 gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
1000 crit_enter();
1001 return (gccb);
1004 static struct gdt_ccb *
1005 gdt_ioctl_cmd(struct gdt_softc *gdt, gdt_ucmd_t *ucmd)
1007 struct gdt_ccb *gccb;
1008 u_int32_t cnt;
1010 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_ioctl_cmd(%p, %p)\n", gdt, ucmd));
1012 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1014 gccb = gdt_get_ccb(gdt);
1015 if (gccb == NULL) {
1016 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
1017 gdt->sc_hanum));
1018 return (gccb);
1020 gccb->gc_ucmd = ucmd;
1021 gccb->gc_service = ucmd->service;
1022 gccb->gc_flags = GDT_GCF_IOCTL;
1024 /* check DPMEM space, copy data buffer from user space */
1025 if (ucmd->service == GDT_CACHESERVICE) {
1026 if (ucmd->OpCode == GDT_IOCTL) {
1027 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_IOCTL_SZ,
1028 sizeof(u_int32_t));
1029 cnt = ucmd->u.ioctl.param_size;
1030 if (cnt > GDT_SCRATCH_SZ) {
1031 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1032 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1033 gdt_free_ccb(gdt, gccb);
1034 return (NULL);
1036 } else {
1037 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1038 GDT_SG_SZ, sizeof(u_int32_t));
1039 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1040 if (cnt > GDT_SCRATCH_SZ) {
1041 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1042 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1043 gdt_free_ccb(gdt, gccb);
1044 return (NULL);
1047 } else {
1048 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1049 GDT_SG_SZ, sizeof(u_int32_t));
1050 cnt = ucmd->u.raw.sdlen;
1051 if (cnt + ucmd->u.raw.sense_len > GDT_SCRATCH_SZ) {
1052 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1053 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt + ucmd->u.raw.sense_len);
1054 gdt_free_ccb(gdt, gccb);
1055 return (NULL);
1058 if (cnt != 0)
1059 bcopy(ucmd->data, gccb->gc_scratch, cnt);
1061 if (gdt->sc_cmd_off + gdt->sc_cmd_len + GDT_DPMEM_COMMAND_OFFSET >
1062 gdt->sc_ic_all_size) {
1063 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_ioctl_cmd(): DPMEM overflow\n",
1064 gdt->sc_hanum));
1065 gdt_free_ccb(gdt, gccb);
1066 return (NULL);
1069 if (gdt->sc_cmd_cnt == 0)
1070 gdt->sc_set_sema0(gdt);
1071 crit_exit();
1073 /* fill cmd structure */
1074 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1075 gccb->gc_cmd_index);
1076 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE,
1077 ucmd->OpCode);
1079 if (ucmd->service == GDT_CACHESERVICE) {
1080 if (ucmd->OpCode == GDT_IOCTL) {
1081 /* IOCTL */
1082 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_PARAM_SIZE,
1083 ucmd->u.ioctl.param_size);
1084 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_SUBFUNC,
1085 ucmd->u.ioctl.subfunc);
1086 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_CHANNEL,
1087 ucmd->u.ioctl.channel);
1088 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
1089 gdt_ccb_vtop(gdt, gccb) +
1090 offsetof(struct gdt_ccb, gc_scratch[0]));
1091 } else {
1092 /* cache service command */
1093 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
1094 ucmd->u.cache.DeviceNo);
1095 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
1096 ucmd->u.cache.BlockNo);
1097 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
1098 ucmd->u.cache.BlockCnt);
1099 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1100 0xffffffffUL);
1101 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1103 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1104 GDT_SG_PTR, gdt_ccb_vtop(gdt, gccb) +
1105 offsetof(struct gdt_ccb, gc_scratch[0]));
1106 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1107 GDT_SG_LEN, ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE);
1109 } else {
1110 /* raw service command */
1111 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
1112 ucmd->u.raw.direction);
1113 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1114 0xffffffffUL);
1115 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
1116 ucmd->u.raw.sdlen);
1117 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
1118 ucmd->u.raw.clen);
1119 bcopy(ucmd->u.raw.cmd, gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
1120 12);
1121 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
1122 ucmd->u.raw.target;
1123 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
1124 ucmd->u.raw.lun;
1125 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
1126 ucmd->u.raw.bus;
1127 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
1128 ucmd->u.raw.sense_len);
1129 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
1130 gdt_ccb_vtop(gdt, gccb) +
1131 offsetof(struct gdt_ccb, gc_scratch[ucmd->u.raw.sdlen]));
1132 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1134 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1135 GDT_SG_PTR, gdt_ccb_vtop(gdt, gccb) +
1136 offsetof(struct gdt_ccb, gc_scratch[0]));
1137 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1138 GDT_SG_LEN, ucmd->u.raw.sdlen);
1141 crit_enter();
1142 gdt_stat.sg_count_act = 1;
1143 gdt->sc_copy_cmd(gdt, gccb);
1144 return (gccb);
1147 static void
1148 gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb)
1150 int t;
1152 t = ccb->ccb_h.target_id;
1153 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cache_cmd(%p, %p, 0x%x, %d)\n",
1154 gdt, ccb, ccb->csio.cdb_io.cdb_bytes[0], t));
1156 switch (ccb->csio.cdb_io.cdb_bytes[0]) {
1157 case TEST_UNIT_READY:
1158 case START_STOP:
1159 break;
1160 case REQUEST_SENSE:
1161 GDT_DPRINTF(GDT_D_MISC, ("REQUEST_SENSE\n"));
1162 break;
1163 case INQUIRY:
1165 struct scsi_inquiry_data *inq;
1167 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
1168 bzero(inq, sizeof(struct scsi_inquiry_data));
1169 inq->device = (gdt->sc_hdr[t].hd_devtype & 4) ?
1170 T_CDROM : T_DIRECT;
1171 inq->dev_qual2 = (gdt->sc_hdr[t].hd_devtype & 1) ? 0x80 : 0;
1172 inq->version = SCSI_REV_2;
1173 inq->response_format = 2;
1174 inq->additional_length = 32;
1175 inq->flags = SID_CmdQue | SID_Sync;
1176 strcpy(inq->vendor, "IIR ");
1177 ksprintf(inq->product, "Host Drive #%02d", t);
1178 strcpy(inq->revision, " ");
1179 break;
1181 case MODE_SENSE_6:
1183 struct mpd_data {
1184 struct scsi_mode_hdr_6 hd;
1185 struct scsi_mode_block_descr bd;
1186 struct scsi_control_page cp;
1187 } *mpd;
1188 u_int8_t page;
1190 mpd = (struct mpd_data *)ccb->csio.data_ptr;
1191 bzero(mpd, sizeof(struct mpd_data));
1192 mpd->hd.datalen = sizeof(struct scsi_mode_hdr_6) +
1193 sizeof(struct scsi_mode_block_descr);
1194 mpd->hd.dev_specific = (gdt->sc_hdr[t].hd_devtype & 2) ? 0x80 : 0;
1195 mpd->hd.block_descr_len = sizeof(struct scsi_mode_block_descr);
1196 mpd->bd.block_len[0] = (GDT_SECTOR_SIZE & 0x00ff0000) >> 16;
1197 mpd->bd.block_len[1] = (GDT_SECTOR_SIZE & 0x0000ff00) >> 8;
1198 mpd->bd.block_len[2] = (GDT_SECTOR_SIZE & 0x000000ff);
1199 page=((struct scsi_mode_sense_6 *)ccb->csio.cdb_io.cdb_bytes)->page;
1200 switch (page) {
1201 default:
1202 GDT_DPRINTF(GDT_D_MISC, ("MODE_SENSE_6: page 0x%x\n", page));
1203 break;
1205 break;
1207 case READ_CAPACITY:
1209 struct scsi_read_capacity_data *rcd;
1211 rcd = (struct scsi_read_capacity_data *)ccb->csio.data_ptr;
1212 bzero(rcd, sizeof(struct scsi_read_capacity_data));
1213 scsi_ulto4b(gdt->sc_hdr[t].hd_size - 1, rcd->addr);
1214 scsi_ulto4b(GDT_SECTOR_SIZE, rcd->length);
1215 break;
1217 default:
1218 GDT_DPRINTF(GDT_D_MISC, ("gdt_internal_cache_cmd(%d) unknown\n",
1219 ccb->csio.cdb_io.cdb_bytes[0]));
1220 break;
1222 ccb->ccb_h.status = CAM_REQ_CMP;
1223 --gdt_stat.io_count_act;
1224 xpt_done(ccb);
1227 static void
1228 gdtmapmem(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1230 bus_addr_t *busaddrp;
1232 busaddrp = (bus_addr_t *)arg;
1233 *busaddrp = dm_segs->ds_addr;
1236 static void
1237 gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1239 struct gdt_ccb *gccb;
1240 union ccb *ccb;
1241 struct gdt_softc *gdt;
1242 int i;
1244 crit_enter();
1246 gccb = (struct gdt_ccb *)arg;
1247 ccb = gccb->gc_ccb;
1248 gdt = cam_sim_softc((struct cam_sim *)ccb->ccb_h.ccb_sim_ptr);
1250 GDT_DPRINTF(GDT_D_CMD, ("gdtexecuteccb(%p, %p, %p, %d, %d)\n",
1251 gdt, gccb, dm_segs, nseg, error));
1252 gdt_stat.sg_count_act = nseg;
1253 if (nseg > gdt_stat.sg_count_max)
1254 gdt_stat.sg_count_max = nseg;
1256 /* Copy the segments into our SG list */
1257 if (gccb->gc_service == GDT_CACHESERVICE) {
1258 for (i = 0; i < nseg; ++i) {
1259 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1260 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1261 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1262 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1263 dm_segs++;
1265 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1266 nseg);
1267 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1268 0xffffffffUL);
1270 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1271 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1272 } else {
1273 for (i = 0; i < nseg; ++i) {
1274 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1275 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1276 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1277 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1278 dm_segs++;
1280 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1281 nseg);
1282 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1283 0xffffffffUL);
1285 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1286 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1289 if (nseg != 0) {
1290 bus_dmasync_op_t op;
1292 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1293 op = BUS_DMASYNC_PREREAD;
1294 else
1295 op = BUS_DMASYNC_PREWRITE;
1296 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap, op);
1299 /* We must NOT abort the command here if CAM_REQ_INPROG is not set,
1300 * because command semaphore is already set!
1303 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1304 /* timeout handling */
1305 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1306 iir_timeout, gccb);
1308 gdt->sc_copy_cmd(gdt, gccb);
1309 crit_exit();
1313 static void
1314 iir_action( struct cam_sim *sim, union ccb *ccb )
1316 struct gdt_softc *gdt;
1317 int bus, target, lun;
1319 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1320 ccb->ccb_h.ccb_sim_ptr = sim;
1321 bus = cam_sim_bus(sim);
1322 target = ccb->ccb_h.target_id;
1323 lun = ccb->ccb_h.target_lun;
1324 GDT_DPRINTF(GDT_D_CMD,
1325 ("iir_action(%p) func 0x%x cmd 0x%x bus %d target %d lun %d\n",
1326 gdt, ccb->ccb_h.func_code, ccb->csio.cdb_io.cdb_bytes[0],
1327 bus, target, lun));
1328 ++gdt_stat.io_count_act;
1329 if (gdt_stat.io_count_act > gdt_stat.io_count_max)
1330 gdt_stat.io_count_max = gdt_stat.io_count_act;
1332 switch (ccb->ccb_h.func_code) {
1333 case XPT_SCSI_IO:
1334 crit_enter();
1335 TAILQ_INSERT_TAIL(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1336 ++gdt_stat.req_queue_act;
1337 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1338 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1339 crit_exit();
1340 gdt_next(gdt);
1341 break;
1342 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1343 case XPT_ABORT: /* Abort the specified CCB */
1344 /* XXX Implement */
1345 ccb->ccb_h.status = CAM_REQ_INVALID;
1346 --gdt_stat.io_count_act;
1347 xpt_done(ccb);
1348 break;
1349 case XPT_SET_TRAN_SETTINGS:
1350 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1351 --gdt_stat.io_count_act;
1352 xpt_done(ccb);
1353 break;
1354 case XPT_GET_TRAN_SETTINGS:
1355 /* Get default/user set transfer settings for the target */
1357 struct ccb_trans_settings *cts;
1358 u_int target_mask;
1360 cts = &ccb->cts;
1361 target_mask = 0x01 << target;
1362 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1363 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
1364 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1365 cts->sync_period = 25; /* 10MHz */
1366 if (cts->sync_period != 0)
1367 cts->sync_offset = 15;
1369 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1370 | CCB_TRANS_SYNC_OFFSET_VALID
1371 | CCB_TRANS_BUS_WIDTH_VALID
1372 | CCB_TRANS_DISC_VALID
1373 | CCB_TRANS_TQ_VALID;
1374 ccb->ccb_h.status = CAM_REQ_CMP;
1375 } else {
1376 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1378 --gdt_stat.io_count_act;
1379 xpt_done(ccb);
1380 break;
1382 case XPT_CALC_GEOMETRY:
1384 struct ccb_calc_geometry *ccg;
1385 u_int32_t secs_per_cylinder;
1387 ccg = &ccb->ccg;
1388 ccg->heads = gdt->sc_hdr[target].hd_heads;
1389 ccg->secs_per_track = gdt->sc_hdr[target].hd_secs;
1390 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1391 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1392 ccb->ccb_h.status = CAM_REQ_CMP;
1393 --gdt_stat.io_count_act;
1394 xpt_done(ccb);
1395 break;
1397 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1399 /* XXX Implement */
1400 ccb->ccb_h.status = CAM_REQ_CMP;
1401 --gdt_stat.io_count_act;
1402 xpt_done(ccb);
1403 break;
1405 case XPT_TERM_IO: /* Terminate the I/O process */
1406 /* XXX Implement */
1407 ccb->ccb_h.status = CAM_REQ_INVALID;
1408 --gdt_stat.io_count_act;
1409 xpt_done(ccb);
1410 break;
1411 case XPT_PATH_INQ: /* Path routing inquiry */
1413 struct ccb_pathinq *cpi = &ccb->cpi;
1415 cpi->version_num = 1;
1416 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1417 cpi->hba_inquiry |= PI_WIDE_16;
1418 cpi->target_sprt = 1;
1419 cpi->hba_misc = 0;
1420 cpi->hba_eng_cnt = 0;
1421 if (bus == gdt->sc_virt_bus)
1422 cpi->max_target = GDT_MAX_HDRIVES - 1;
1423 else if (gdt->sc_class & GDT_FC)
1424 cpi->max_target = GDT_MAXID_FC - 1;
1425 else
1426 cpi->max_target = GDT_MAXID - 1;
1427 cpi->max_lun = 7;
1428 cpi->unit_number = cam_sim_unit(sim);
1429 cpi->bus_id = bus;
1430 cpi->initiator_id =
1431 (bus == gdt->sc_virt_bus ? 127 : gdt->sc_bus_id[bus]);
1432 cpi->base_transfer_speed = 3300;
1433 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1434 strncpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN);
1435 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1436 cpi->ccb_h.status = CAM_REQ_CMP;
1437 --gdt_stat.io_count_act;
1438 xpt_done(ccb);
1439 break;
1441 default:
1442 GDT_DPRINTF(GDT_D_INVALID, ("gdt_next(%p) cmd 0x%x invalid\n",
1443 gdt, ccb->ccb_h.func_code));
1444 ccb->ccb_h.status = CAM_REQ_INVALID;
1445 --gdt_stat.io_count_act;
1446 xpt_done(ccb);
1447 break;
1451 static void
1452 iir_poll( struct cam_sim *sim )
1454 struct gdt_softc *gdt;
1456 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1457 GDT_DPRINTF(GDT_D_CMD, ("iir_poll sim %p gdt %p\n", sim, gdt));
1458 iir_intr(gdt);
1461 static void
1462 iir_timeout(void *arg)
1464 GDT_DPRINTF(GDT_D_TIMEOUT, ("iir_timeout(%p)\n", arg));
1467 static void
1468 iir_watchdog(void *arg)
1470 struct gdt_softc *gdt;
1472 gdt = (struct gdt_softc *)arg;
1473 GDT_DPRINTF(GDT_D_DEBUG, ("iir_watchdog(%p)\n", gdt));
1476 int ccbs = 0, ucmds = 0, frees = 0, pends = 0;
1477 struct gdt_ccb *p;
1478 struct ccb_hdr *h;
1479 struct gdt_ucmd *u;
1481 for (h = TAILQ_FIRST(&gdt->sc_ccb_queue); h != NULL;
1482 h = TAILQ_NEXT(h, sim_links.tqe))
1483 ccbs++;
1484 for (u = TAILQ_FIRST(&gdt->sc_ucmd_queue); u != NULL;
1485 u = TAILQ_NEXT(u, links))
1486 ucmds++;
1487 for (p = SLIST_FIRST(&gdt->sc_free_gccb); p != NULL;
1488 p = SLIST_NEXT(p, sle))
1489 frees++;
1490 for (p = SLIST_FIRST(&gdt->sc_pending_gccb); p != NULL;
1491 p = SLIST_NEXT(p, sle))
1492 pends++;
1494 GDT_DPRINTF(GDT_D_TIMEOUT, ("ccbs %d ucmds %d frees %d pends %d\n",
1495 ccbs, ucmds, frees, pends));
1498 callout_reset(&gdt->watchdog_timer, hz * 15, iir_watchdog, gdt);
1501 static void
1502 iir_shutdown( void *arg, int howto )
1504 struct gdt_softc *gdt;
1505 struct gdt_ccb *gccb;
1506 gdt_ucmd_t *ucmd;
1507 int i;
1509 gdt = (struct gdt_softc *)arg;
1510 GDT_DPRINTF(GDT_D_CMD, ("iir_shutdown(%p, %d)\n", gdt, howto));
1512 printf("iir%d: Flushing all Host Drives. Please wait ... ",
1513 gdt->sc_hanum);
1515 /* allocate ucmd buffer */
1516 ucmd = kmalloc(sizeof(gdt_ucmd_t), M_DEVBUF, M_INTWAIT | M_ZERO);
1518 /* wait for pending IOs */
1519 crit_enter();
1520 gdt->sc_state = GDT_SHUTDOWN;
1521 crit_exit();
1522 if ((gccb = SLIST_FIRST(&gdt->sc_pending_gccb)) != NULL)
1523 (void) tsleep((void *)gccb, PCATCH, "iirshw", 100 * hz);
1525 /* flush */
1526 for (i = 0; i < GDT_MAX_HDRIVES; ++i) {
1527 if (gdt->sc_hdr[i].hd_present) {
1528 ucmd->service = GDT_CACHESERVICE;
1529 ucmd->OpCode = GDT_FLUSH;
1530 ucmd->u.cache.DeviceNo = i;
1531 crit_enter();
1532 TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
1533 ucmd->complete_flag = FALSE;
1534 crit_exit();
1535 gdt_next(gdt);
1536 if (!ucmd->complete_flag)
1537 (void) tsleep((void *)ucmd, PCATCH, "iirshw", 10*hz);
1541 kfree(ucmd, M_DEVBUF);
1542 printf("Done.\n");
1545 void
1546 iir_intr(void *arg)
1548 struct gdt_softc *gdt = arg;
1549 struct gdt_intr_ctx ctx;
1550 struct gdt_ccb *gccb;
1551 gdt_ucmd_t *ucmd;
1552 u_int32_t cnt;
1554 GDT_DPRINTF(GDT_D_INTR, ("gdt_intr(%p)\n", gdt));
1556 /* If polling and we were not called from gdt_wait, just return */
1557 if ((gdt->sc_state & GDT_POLLING) &&
1558 !(gdt->sc_state & GDT_POLL_WAIT))
1559 return;
1561 if (!(gdt->sc_state & GDT_POLLING))
1562 crit_enter();
1563 gdt_wait_index = 0;
1565 ctx.istatus = gdt->sc_get_status(gdt);
1566 if (!ctx.istatus) {
1567 if (!(gdt->sc_state & GDT_POLLING))
1568 crit_exit();
1569 gdt->sc_status = GDT_S_NO_STATUS;
1570 return;
1573 gdt->sc_intr(gdt, &ctx);
1575 gdt->sc_status = ctx.cmd_status;
1576 gdt->sc_service = ctx.service;
1577 gdt->sc_info = ctx.info;
1578 gdt->sc_info2 = ctx.info2;
1580 if (gdt->sc_state & GDT_POLL_WAIT) {
1581 gdt_wait_gdt = gdt;
1582 gdt_wait_index = ctx.istatus;
1585 if (ctx.istatus == GDT_ASYNCINDEX) {
1586 gdt_async_event(gdt, ctx.service);
1587 if (!(gdt->sc_state & GDT_POLLING))
1588 crit_exit();
1589 return;
1591 if (ctx.istatus == GDT_SPEZINDEX) {
1592 GDT_DPRINTF(GDT_D_INVALID,
1593 ("iir%d: Service unknown or not initialized!\n",
1594 gdt->sc_hanum));
1595 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1596 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1597 gdt_store_event(GDT_ES_DRIVER, 4, &gdt->sc_dvr);
1598 if (!(gdt->sc_state & GDT_POLLING))
1599 crit_exit();
1600 return;
1603 gccb = &gdt->sc_gccbs[ctx.istatus - 2];
1604 ctx.service = gccb->gc_service;
1606 switch (gccb->gc_flags) {
1607 case GDT_GCF_UNUSED:
1608 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: Index (%d) to unused command!\n",
1609 gdt->sc_hanum, ctx.istatus));
1610 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1611 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1612 gdt->sc_dvr.eu.driver.index = ctx.istatus;
1613 gdt_store_event(GDT_ES_DRIVER, 1, &gdt->sc_dvr);
1614 gdt_free_ccb(gdt, gccb);
1615 /* fallthrough */
1617 case GDT_GCF_INTERNAL:
1618 if (!(gdt->sc_state & GDT_POLLING))
1619 crit_exit();
1620 break;
1622 case GDT_GCF_IOCTL:
1623 ucmd = gccb->gc_ucmd;
1624 if (gdt->sc_status == GDT_S_BSY) {
1625 GDT_DPRINTF(GDT_D_DEBUG, ("iir_intr(%p) ioctl: gccb %p busy\n",
1626 gdt, gccb));
1627 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
1628 if (!(gdt->sc_state & GDT_POLLING))
1629 crit_exit();
1630 } else {
1631 ucmd->status = gdt->sc_status;
1632 ucmd->info = gdt->sc_info;
1633 ucmd->complete_flag = TRUE;
1634 if (ucmd->service == GDT_CACHESERVICE) {
1635 if (ucmd->OpCode == GDT_IOCTL) {
1636 cnt = ucmd->u.ioctl.param_size;
1637 if (cnt != 0)
1638 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1639 } else {
1640 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1641 if (cnt != 0)
1642 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1644 } else {
1645 cnt = ucmd->u.raw.sdlen;
1646 if (cnt != 0)
1647 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1648 if (ucmd->u.raw.sense_len != 0)
1649 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1651 gdt_free_ccb(gdt, gccb);
1652 if (!(gdt->sc_state & GDT_POLLING))
1653 crit_exit();
1654 /* wakeup */
1655 wakeup(ucmd);
1657 gdt_next(gdt);
1658 break;
1660 default:
1661 gdt_free_ccb(gdt, gccb);
1662 gdt_sync_event(gdt, ctx.service, ctx.istatus, gccb);
1663 if (!(gdt->sc_state & GDT_POLLING))
1664 crit_exit();
1665 gdt_next(gdt);
1666 break;
1671 gdt_async_event(struct gdt_softc *gdt, int service)
1673 struct gdt_ccb *gccb;
1675 GDT_DPRINTF(GDT_D_INTR, ("gdt_async_event(%p, %d)\n", gdt, service));
1677 if (service == GDT_SCREENSERVICE) {
1678 if (gdt->sc_status == GDT_MSG_REQUEST) {
1679 while (gdt->sc_test_busy(gdt))
1680 DELAY(1);
1681 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1682 gccb = gdt_get_ccb(gdt);
1683 if (gccb == NULL) {
1684 printf("iir%d: No free command index found\n",
1685 gdt->sc_hanum);
1686 return (1);
1688 gccb->gc_service = service;
1689 gccb->gc_flags = GDT_GCF_SCREEN;
1690 gdt->sc_set_sema0(gdt);
1691 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1692 gccb->gc_cmd_index);
1693 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_READ);
1694 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1695 GDT_MSG_INV_HANDLE);
1696 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1697 gdt_ccb_vtop(gdt, gccb) +
1698 offsetof(struct gdt_ccb, gc_scratch[0]));
1699 gdt->sc_cmd_off = 0;
1700 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1701 sizeof(u_int32_t));
1702 gdt->sc_cmd_cnt = 0;
1703 gdt->sc_copy_cmd(gdt, gccb);
1704 printf("iir%d: [PCI %d/%d] ",
1705 gdt->sc_hanum,gdt->sc_bus,gdt->sc_slot);
1706 gdt->sc_release_event(gdt);
1709 } else {
1710 if ((gdt->sc_fw_vers & 0xff) >= 0x1a) {
1711 gdt->sc_dvr.size = 0;
1712 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1713 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1714 /* severity and event_string already set! */
1715 } else {
1716 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.async);
1717 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1718 gdt->sc_dvr.eu.async.service = service;
1719 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1720 gdt->sc_dvr.eu.async.info = gdt->sc_info;
1721 *(u_int32_t *)gdt->sc_dvr.eu.async.scsi_coord = gdt->sc_info2;
1723 gdt_store_event(GDT_ES_ASYNC, service, &gdt->sc_dvr);
1724 printf("iir%d: %s\n", gdt->sc_hanum, gdt->sc_dvr.event_string);
1727 return (0);
1731 gdt_sync_event(struct gdt_softc *gdt, int service,
1732 u_int8_t index, struct gdt_ccb *gccb)
1734 union ccb *ccb;
1735 bus_dmasync_op_t op;
1737 GDT_DPRINTF(GDT_D_INTR,
1738 ("gdt_sync_event(%p, %d, %d, %p)\n", gdt,service,index,gccb));
1740 ccb = gccb->gc_ccb;
1742 if (service == GDT_SCREENSERVICE) {
1743 u_int32_t msg_len;
1745 msg_len = gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_LEN);
1746 if (msg_len)
1747 if (!(gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1748 gccb->gc_scratch[GDT_SCR_MSG_EXT])) {
1749 gccb->gc_scratch[GDT_SCR_MSG_TEXT + msg_len] = '\0';
1750 printf("%s",&gccb->gc_scratch[GDT_SCR_MSG_TEXT]);
1753 if (gccb->gc_scratch[GDT_SCR_MSG_EXT] &&
1754 !gccb->gc_scratch[GDT_SCR_MSG_ANSWER]) {
1755 while (gdt->sc_test_busy(gdt))
1756 DELAY(1);
1757 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1758 gccb = gdt_get_ccb(gdt);
1759 if (gccb == NULL) {
1760 printf("iir%d: No free command index found\n",
1761 gdt->sc_hanum);
1762 return (1);
1764 gccb->gc_service = service;
1765 gccb->gc_flags = GDT_GCF_SCREEN;
1766 gdt->sc_set_sema0(gdt);
1767 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1768 gccb->gc_cmd_index);
1769 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_READ);
1770 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1771 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1772 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1773 gdt_ccb_vtop(gdt, gccb) +
1774 offsetof(struct gdt_ccb, gc_scratch[0]));
1775 gdt->sc_cmd_off = 0;
1776 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1777 sizeof(u_int32_t));
1778 gdt->sc_cmd_cnt = 0;
1779 gdt->sc_copy_cmd(gdt, gccb);
1780 gdt->sc_release_event(gdt);
1781 return (0);
1784 if (gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1785 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN)) {
1786 /* default answers (getchar() not possible) */
1787 if (gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) == 1) {
1788 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN, 0);
1789 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 1);
1790 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 0;
1791 } else {
1792 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN,
1793 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) - 2);
1794 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 2);
1795 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 1;
1796 gccb->gc_scratch[GDT_SCR_MSG_TEXT + 1] = 0;
1798 gccb->gc_scratch[GDT_SCR_MSG_EXT] = 0;
1799 gccb->gc_scratch[GDT_SCR_MSG_ANSWER] = 0;
1800 while (gdt->sc_test_busy(gdt))
1801 DELAY(1);
1802 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1803 gccb = gdt_get_ccb(gdt);
1804 if (gccb == NULL) {
1805 printf("iir%d: No free command index found\n",
1806 gdt->sc_hanum);
1807 return (1);
1809 gccb->gc_service = service;
1810 gccb->gc_flags = GDT_GCF_SCREEN;
1811 gdt->sc_set_sema0(gdt);
1812 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1813 gccb->gc_cmd_index);
1814 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
1815 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1816 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1817 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1818 gdt_ccb_vtop(gdt, gccb) +
1819 offsetof(struct gdt_ccb, gc_scratch[0]));
1820 gdt->sc_cmd_off = 0;
1821 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1822 sizeof(u_int32_t));
1823 gdt->sc_cmd_cnt = 0;
1824 gdt->sc_copy_cmd(gdt, gccb);
1825 gdt->sc_release_event(gdt);
1826 return (0);
1828 printf("\n");
1829 return (0);
1830 } else {
1831 callout_stop(&ccb->ccb_h.timeout_ch);
1832 if (gdt->sc_status == GDT_S_BSY) {
1833 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_sync_event(%p) gccb %p busy\n",
1834 gdt, gccb));
1835 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1836 ++gdt_stat.req_queue_act;
1837 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1838 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1839 return (2);
1842 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1843 op = BUS_DMASYNC_POSTREAD;
1844 else
1845 op = BUS_DMASYNC_POSTWRITE;
1846 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap, op);
1848 ccb->csio.resid = 0;
1849 if (gdt->sc_status == GDT_S_OK) {
1850 ccb->ccb_h.status = CAM_REQ_CMP;
1851 } else {
1852 /* error */
1853 if (gccb->gc_service == GDT_CACHESERVICE) {
1854 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1855 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1856 bzero(&ccb->csio.sense_data, ccb->csio.sense_len);
1857 ccb->csio.sense_data.error_code =
1858 SSD_CURRENT_ERROR | SSD_ERRCODE_VALID;
1859 ccb->csio.sense_data.flags = SSD_KEY_NOT_READY;
1861 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.sync);
1862 gdt->sc_dvr.eu.sync.ionode = gdt->sc_hanum;
1863 gdt->sc_dvr.eu.sync.service = service;
1864 gdt->sc_dvr.eu.sync.status = gdt->sc_status;
1865 gdt->sc_dvr.eu.sync.info = gdt->sc_info;
1866 gdt->sc_dvr.eu.sync.hostdrive = ccb->ccb_h.target_id;
1867 if (gdt->sc_status >= 0x8000)
1868 gdt_store_event(GDT_ES_SYNC, 0, &gdt->sc_dvr);
1869 else
1870 gdt_store_event(GDT_ES_SYNC, service, &gdt->sc_dvr);
1871 } else {
1872 /* raw service */
1873 if (gdt->sc_status != GDT_S_RAW_SCSI || gdt->sc_info >= 0x100) {
1874 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1875 } else {
1876 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
1877 ccb->csio.scsi_status = gdt->sc_info;
1878 bcopy(gccb->gc_scratch, &ccb->csio.sense_data,
1879 ccb->csio.sense_len);
1883 --gdt_stat.io_count_act;
1884 xpt_done(ccb);
1886 return (0);
1889 /* Controller event handling functions */
1890 gdt_evt_str *
1891 gdt_store_event(u_int16_t source, u_int16_t idx, gdt_evt_data *evt)
1893 gdt_evt_str *e;
1894 struct timeval tv;
1896 GDT_DPRINTF(GDT_D_MISC, ("gdt_store_event(%d, %d)\n", source, idx));
1897 if (source == 0) /* no source -> no event */
1898 return 0;
1900 if (ebuffer[elastidx].event_source == source &&
1901 ebuffer[elastidx].event_idx == idx &&
1902 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
1903 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
1904 (char *)&evt->eu, evt->size)) ||
1905 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
1906 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
1907 (char *)&evt->event_string)))) {
1908 e = &ebuffer[elastidx];
1909 getmicrotime(&tv);
1910 e->last_stamp = tv.tv_sec;
1911 ++e->same_count;
1912 } else {
1913 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
1914 ++elastidx;
1915 if (elastidx == GDT_MAX_EVENTS)
1916 elastidx = 0;
1917 if (elastidx == eoldidx) { /* reached mark ? */
1918 ++eoldidx;
1919 if (eoldidx == GDT_MAX_EVENTS)
1920 eoldidx = 0;
1923 e = &ebuffer[elastidx];
1924 e->event_source = source;
1925 e->event_idx = idx;
1926 getmicrotime(&tv);
1927 e->first_stamp = e->last_stamp = tv.tv_sec;
1928 e->same_count = 1;
1929 e->event_data = *evt;
1930 e->application = 0;
1932 return e;
1936 gdt_read_event(int handle, gdt_evt_str *estr)
1938 gdt_evt_str *e;
1939 int eindex;
1941 GDT_DPRINTF(GDT_D_MISC, ("gdt_read_event(%d)\n", handle));
1942 crit_enter();
1943 if (handle == -1)
1944 eindex = eoldidx;
1945 else
1946 eindex = handle;
1947 estr->event_source = 0;
1949 if (eindex >= GDT_MAX_EVENTS) {
1950 crit_exit();
1951 return eindex;
1953 e = &ebuffer[eindex];
1954 if (e->event_source != 0) {
1955 if (eindex != elastidx) {
1956 if (++eindex == GDT_MAX_EVENTS)
1957 eindex = 0;
1958 } else {
1959 eindex = -1;
1961 memcpy(estr, e, sizeof(gdt_evt_str));
1963 crit_exit();
1964 return eindex;
1967 void
1968 gdt_readapp_event(u_int8_t application, gdt_evt_str *estr)
1970 gdt_evt_str *e;
1971 int found = FALSE;
1972 int eindex;
1974 GDT_DPRINTF(GDT_D_MISC, ("gdt_readapp_event(%d)\n", application));
1975 crit_enter();
1976 eindex = eoldidx;
1977 for (;;) {
1978 e = &ebuffer[eindex];
1979 if (e->event_source == 0)
1980 break;
1981 if ((e->application & application) == 0) {
1982 e->application |= application;
1983 found = TRUE;
1984 break;
1986 if (eindex == elastidx)
1987 break;
1988 if (++eindex == GDT_MAX_EVENTS)
1989 eindex = 0;
1991 if (found)
1992 memcpy(estr, e, sizeof(gdt_evt_str));
1993 else
1994 estr->event_source = 0;
1995 crit_exit();
1998 void
1999 gdt_clear_events(void)
2001 GDT_DPRINTF(GDT_D_MISC, ("gdt_clear_events\n"));
2003 eoldidx = elastidx = 0;
2004 ebuffer[0].event_source = 0;