2 * Copyright (c) 2000-04 ICP vortex GmbH
3 * Copyright (c) 2002-04 Intel Corporation
4 * Copyright (c) 2003-04 Adaptec Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
33 * iir.c: SCSI dependant code for the Intel Integrated RAID Controller driver
35 * Written by: Achim Leubner <achim_leubner@adaptec.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 * $Id: iir.c 1.5 2004/03/30 10:17:53 achim Exp $"
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
50 /* #include "opt_iir.h" */
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/endian.h>
54 #include <sys/eventhandler.h>
55 #include <sys/malloc.h>
56 #include <sys/kernel.h>
59 #include <machine/bus_memio.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus.h>
62 #include <machine/clock.h>
63 #include <machine/stdarg.h>
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_xpt_sim.h>
69 #include <cam/cam_debug.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
76 #include <dev/iir/iir.h>
78 struct gdt_softc *gdt_wait_gdt;
82 int gdt_debug = GDT_DEBUG;
84 #define MAX_SERBUF 160
85 static void ser_init(void);
86 static void ser_puts(char *str);
87 static void ser_putc(int c);
88 static char strbuf[MAX_SERBUF+1];
90 #define COM_BASE 0x2f8
92 #define COM_BASE 0x3f8
94 static void ser_init()
96 unsigned port=COM_BASE;
100 /* 19200 Baud, if 9600: outb(12,port) */
106 static void ser_puts(char *str)
111 for (ptr=str;*ptr;++ptr)
112 ser_putc((int)(*ptr));
115 static void ser_putc(int c)
117 unsigned port=COM_BASE;
119 while ((inb(port+5) & 0x20)==0);
123 while ((inb(port+5) & 0x20)==0);
128 int ser_printf(const char *fmt, ...)
134 i = vsprintf(strbuf,fmt,args);
142 /* The linked list of softc structures */
143 struct gdt_softc_list gdt_softcs = TAILQ_HEAD_INITIALIZER(gdt_softcs);
144 /* controller cnt. */
147 static gdt_evt_str ebuffer[GDT_MAX_EVENTS];
148 static int elastidx, eoldidx;
150 gdt_statist_t gdt_stat;
152 /* Definitions for our use of the SIM private CCB area */
153 #define ccb_sim_ptr spriv_ptr0
154 #define ccb_priority spriv_field1
156 static void iir_action(struct cam_sim *sim, union ccb *ccb);
157 static void iir_poll(struct cam_sim *sim);
158 static void iir_shutdown(void *arg, int howto);
159 static void iir_timeout(void *arg);
160 static void iir_watchdog(void *arg);
162 static void gdt_eval_mapping(u_int32_t size, int *cyls, int *heads,
164 static int gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
165 u_int8_t service, u_int16_t opcode,
166 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3);
167 static int gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *ccb,
170 static struct gdt_ccb *gdt_get_ccb(struct gdt_softc *gdt);
171 static u_int32_t gdt_ccb_vtop(struct gdt_softc *gdt,
172 struct gdt_ccb *gccb);
174 static int gdt_sync_event(struct gdt_softc *gdt, int service,
175 u_int8_t index, struct gdt_ccb *gccb);
176 static int gdt_async_event(struct gdt_softc *gdt, int service);
177 static struct gdt_ccb *gdt_raw_cmd(struct gdt_softc *gdt,
178 union ccb *ccb, int *lock);
179 static struct gdt_ccb *gdt_cache_cmd(struct gdt_softc *gdt,
180 union ccb *ccb, int *lock);
181 static struct gdt_ccb *gdt_ioctl_cmd(struct gdt_softc *gdt,
182 gdt_ucmd_t *ucmd, int *lock);
183 static void gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb);
185 static void gdtmapmem(void *arg, bus_dma_segment_t *dm_segs,
186 int nseg, int error);
187 static void gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
188 int nseg, int error);
191 iir_init(struct gdt_softc *gdt)
194 int i, id, drv_cyls, drv_hds, drv_secs;
195 struct gdt_ccb *gccb;
197 GDT_DPRINTF(GDT_D_DEBUG, ("iir_init()\n"));
199 gdt->sc_state = GDT_POLLING;
201 bzero(&gdt_stat, sizeof(gdt_statist_t));
203 SLIST_INIT(&gdt->sc_free_gccb);
204 SLIST_INIT(&gdt->sc_pending_gccb);
205 TAILQ_INIT(&gdt->sc_ccb_queue);
206 TAILQ_INIT(&gdt->sc_ucmd_queue);
207 TAILQ_INSERT_TAIL(&gdt_softcs, gdt, links);
209 /* DMA tag for mapping buffers into device visible space. */
210 if (bus_dma_tag_create(gdt->sc_parent_dmat, /*alignment*/1, /*boundary*/0,
211 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
212 /*highaddr*/BUS_SPACE_MAXADDR,
213 /*filter*/NULL, /*filterarg*/NULL,
214 /*maxsize*/MAXBSIZE, /*nsegments*/GDT_MAXSG,
215 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
216 /*flags*/BUS_DMA_ALLOCNOW,
217 /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
218 &gdt->sc_buffer_dmat) != 0) {
219 printf("iir%d: bus_dma_tag_create(...,gdt->sc_buffer_dmat) failed\n",
223 gdt->sc_init_level++;
225 /* DMA tag for our ccb structures */
226 if (bus_dma_tag_create(gdt->sc_parent_dmat,
229 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
230 /*highaddr*/BUS_SPACE_MAXADDR,
233 GDT_MAXCMDS * sizeof(struct gdt_ccb), /* maxsize */
235 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
236 /*flags*/0, /*lockfunc*/busdma_lock_mutex,
237 /*lockarg*/&Giant, &gdt->sc_gccb_dmat) != 0) {
238 printf("iir%d: bus_dma_tag_create(...,gdt->sc_gccb_dmat) failed\n",
242 gdt->sc_init_level++;
244 /* Allocation for our ccbs */
245 if (bus_dmamem_alloc(gdt->sc_gccb_dmat, (void **)&gdt->sc_gccbs,
246 BUS_DMA_NOWAIT, &gdt->sc_gccb_dmamap) != 0) {
247 printf("iir%d: bus_dmamem_alloc(...,&gdt->sc_gccbs,...) failed\n",
251 gdt->sc_init_level++;
253 /* And permanently map them */
254 bus_dmamap_load(gdt->sc_gccb_dmat, gdt->sc_gccb_dmamap,
255 gdt->sc_gccbs, GDT_MAXCMDS * sizeof(struct gdt_ccb),
256 gdtmapmem, &gdt->sc_gccb_busbase, /*flags*/0);
257 gdt->sc_init_level++;
259 /* Clear them out. */
260 bzero(gdt->sc_gccbs, GDT_MAXCMDS * sizeof(struct gdt_ccb));
262 /* Initialize the ccbs */
263 for (i = GDT_MAXCMDS-1; i >= 0; i--) {
264 gdt->sc_gccbs[i].gc_cmd_index = i + 2;
265 gdt->sc_gccbs[i].gc_flags = GDT_GCF_UNUSED;
266 gdt->sc_gccbs[i].gc_map_flag = FALSE;
267 if (bus_dmamap_create(gdt->sc_buffer_dmat, /*flags*/0,
268 &gdt->sc_gccbs[i].gc_dmamap) != 0)
270 gdt->sc_gccbs[i].gc_map_flag = TRUE;
271 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, &gdt->sc_gccbs[i], sle);
273 gdt->sc_init_level++;
275 /* create the control device */
276 gdt->sc_dev = gdt_make_dev(gdt->sc_hanum);
278 /* allocate ccb for gdt_internal_cmd() */
279 gccb = gdt_get_ccb(gdt);
281 printf("iir%d: No free command index found\n",
286 if (!gdt_internal_cmd(gdt, gccb, GDT_SCREENSERVICE, GDT_INIT,
288 printf("iir%d: Screen service initialization error %d\n",
289 gdt->sc_hanum, gdt->sc_status);
290 gdt_free_ccb(gdt, gccb);
294 gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_UNFREEZE_IO,
297 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INIT,
298 GDT_LINUX_OS, 0, 0)) {
299 printf("iir%d: Cache service initialization error %d\n",
300 gdt->sc_hanum, gdt->sc_status);
301 gdt_free_ccb(gdt, gccb);
304 cdev_cnt = (u_int16_t)gdt->sc_info;
305 gdt->sc_fw_vers = gdt->sc_service;
307 /* Detect number of buses */
308 gdt_enc32(gccb->gc_scratch + GDT_IOC_VERSION, GDT_IOC_NEWEST);
309 gccb->gc_scratch[GDT_IOC_LIST_ENTRIES] = GDT_MAXBUS;
310 gccb->gc_scratch[GDT_IOC_FIRST_CHAN] = 0;
311 gccb->gc_scratch[GDT_IOC_LAST_CHAN] = GDT_MAXBUS - 1;
312 gdt_enc32(gccb->gc_scratch + GDT_IOC_LIST_OFFSET, GDT_IOC_HDR_SZ);
313 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
314 GDT_IOCHAN_RAW_DESC, GDT_INVALID_CHANNEL,
315 GDT_IOC_HDR_SZ + GDT_MAXBUS * GDT_RAWIOC_SZ)) {
316 gdt->sc_bus_cnt = gccb->gc_scratch[GDT_IOC_CHAN_COUNT];
317 for (i = 0; i < gdt->sc_bus_cnt; i++) {
318 id = gccb->gc_scratch[GDT_IOC_HDR_SZ +
319 i * GDT_RAWIOC_SZ + GDT_RAWIOC_PROC_ID];
320 gdt->sc_bus_id[i] = id < GDT_MAXID_FC ? id : 0xff;
323 /* New method failed, use fallback. */
324 for (i = 0; i < GDT_MAXBUS; i++) {
325 gdt_enc32(gccb->gc_scratch + GDT_GETCH_CHANNEL_NO, i);
326 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
327 GDT_SCSI_CHAN_CNT | GDT_L_CTRL_PATTERN,
328 GDT_IO_CHANNEL | GDT_INVALID_CHANNEL,
331 printf("iir%d: Cannot get channel count, "
332 "error %d\n", gdt->sc_hanum, gdt->sc_status);
333 gdt_free_ccb(gdt, gccb);
339 (gccb->gc_scratch[GDT_GETCH_SIOP_ID] < GDT_MAXID_FC) ?
340 gccb->gc_scratch[GDT_GETCH_SIOP_ID] : 0xff;
344 /* add one "virtual" channel for the host drives */
345 gdt->sc_virt_bus = gdt->sc_bus_cnt;
348 if (!gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_INIT,
350 printf("iir%d: Raw service initialization error %d\n",
351 gdt->sc_hanum, gdt->sc_status);
352 gdt_free_ccb(gdt, gccb);
356 /* Set/get features raw service (scatter/gather) */
357 gdt->sc_raw_feat = 0;
358 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_SET_FEAT,
359 GDT_SCATTER_GATHER, 0, 0)) {
360 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_GET_FEAT,
362 gdt->sc_raw_feat = gdt->sc_info;
363 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
364 panic("iir%d: Scatter/Gather Raw Service "
365 "required but not supported!\n", gdt->sc_hanum);
366 gdt_free_ccb(gdt, gccb);
372 /* Set/get features cache service (scatter/gather) */
373 gdt->sc_cache_feat = 0;
374 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_SET_FEAT,
375 0, GDT_SCATTER_GATHER, 0)) {
376 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_GET_FEAT,
378 gdt->sc_cache_feat = gdt->sc_info;
379 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
380 panic("iir%d: Scatter/Gather Cache Service "
381 "required but not supported!\n", gdt->sc_hanum);
382 gdt_free_ccb(gdt, gccb);
389 gdt_enc32(gccb->gc_scratch + GDT_OEM_VERSION, 0x01);
390 gdt_enc32(gccb->gc_scratch + GDT_OEM_BUFSIZE, sizeof(gdt_oem_record_t));
391 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
392 GDT_OEM_STR_RECORD, GDT_INVALID_CHANNEL,
393 sizeof(gdt_oem_str_record_t))) {
394 strncpy(gdt->oem_name, ((gdt_oem_str_record_t *)
395 gccb->gc_scratch)->text.scsi_host_drive_inquiry_vendor_id, 7);
396 gdt->oem_name[7]='\0';
398 /* Old method, based on PCI ID */
399 if (gdt->sc_vendor == INTEL_VENDOR_ID)
400 strcpy(gdt->oem_name,"Intel ");
402 strcpy(gdt->oem_name,"ICP ");
405 /* Scan for cache devices */
406 for (i = 0; i < cdev_cnt && i < GDT_MAX_HDRIVES; i++) {
407 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INFO,
409 gdt->sc_hdr[i].hd_present = 1;
410 gdt->sc_hdr[i].hd_size = gdt->sc_info;
413 * Evaluate mapping (sectors per head, heads per cyl)
415 gdt->sc_hdr[i].hd_size &= ~GDT_SECS32;
416 if (gdt->sc_info2 == 0)
417 gdt_eval_mapping(gdt->sc_hdr[i].hd_size,
418 &drv_cyls, &drv_hds, &drv_secs);
420 drv_hds = gdt->sc_info2 & 0xff;
421 drv_secs = (gdt->sc_info2 >> 8) & 0xff;
422 drv_cyls = gdt->sc_hdr[i].hd_size / drv_hds /
425 gdt->sc_hdr[i].hd_heads = drv_hds;
426 gdt->sc_hdr[i].hd_secs = drv_secs;
428 gdt->sc_hdr[i].hd_size = drv_cyls * drv_hds * drv_secs;
430 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE,
431 GDT_DEVTYPE, i, 0, 0))
432 gdt->sc_hdr[i].hd_devtype = gdt->sc_info;
436 GDT_DPRINTF(GDT_D_INIT, ("dpmem %x %d-bus %d cache device%s\n",
438 gdt->sc_bus_cnt, cdev_cnt,
439 cdev_cnt == 1 ? "" : "s"));
440 gdt_free_ccb(gdt, gccb);
447 iir_free(struct gdt_softc *gdt)
451 GDT_DPRINTF(GDT_D_INIT, ("iir_free()\n"));
453 switch (gdt->sc_init_level) {
455 gdt_destroy_dev(gdt->sc_dev);
457 for (i = GDT_MAXCMDS-1; i >= 0; i--)
458 if (gdt->sc_gccbs[i].gc_map_flag)
459 bus_dmamap_destroy(gdt->sc_buffer_dmat,
460 gdt->sc_gccbs[i].gc_dmamap);
461 bus_dmamap_unload(gdt->sc_gccb_dmat, gdt->sc_gccb_dmamap);
463 bus_dmamem_free(gdt->sc_gccb_dmat, gdt->sc_gccbs, gdt->sc_gccb_dmamap);
465 bus_dma_tag_destroy(gdt->sc_gccb_dmat);
467 bus_dma_tag_destroy(gdt->sc_buffer_dmat);
469 bus_dma_tag_destroy(gdt->sc_parent_dmat);
473 TAILQ_REMOVE(&gdt_softcs, gdt, links);
477 iir_attach(struct gdt_softc *gdt)
479 struct cam_devq *devq;
482 GDT_DPRINTF(GDT_D_INIT, ("iir_attach()\n"));
485 * Create the device queue for our SIM.
487 devq = cam_simq_alloc(GDT_MAXCMDS);
491 for (i = 0; i < gdt->sc_bus_cnt; i++) {
493 * Construct our SIM entry
495 gdt->sims[i] = cam_sim_alloc(iir_action, iir_poll, "iir",
496 gdt, gdt->sc_hanum, /*untagged*/2,
497 /*tagged*/GDT_MAXCMDS, devq);
498 if (xpt_bus_register(gdt->sims[i], i) != CAM_SUCCESS) {
499 cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
503 if (xpt_create_path(&gdt->paths[i], /*periph*/NULL,
504 cam_sim_path(gdt->sims[i]),
506 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
507 xpt_bus_deregister(cam_sim_path(gdt->sims[i]));
508 cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
513 EVENTHANDLER_REGISTER(shutdown_final, iir_shutdown,
514 gdt, SHUTDOWN_PRI_DEFAULT);
515 /* iir_watchdog(gdt); */
516 gdt->sc_state = GDT_NORMAL;
520 gdt_eval_mapping(u_int32_t size, int *cyls, int *heads, int *secs)
522 *cyls = size / GDT_HEADS / GDT_SECS;
523 if (*cyls < GDT_MAXCYLS) {
527 /* Too high for 64 * 32 */
528 *cyls = size / GDT_MEDHEADS / GDT_MEDSECS;
529 if (*cyls < GDT_MAXCYLS) {
530 *heads = GDT_MEDHEADS;
533 /* Too high for 127 * 63 */
534 *cyls = size / GDT_BIGHEADS / GDT_BIGSECS;
535 *heads = GDT_BIGHEADS;
542 gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *gccb,
547 GDT_DPRINTF(GDT_D_INIT,
548 ("gdt_wait(%p, %p, %d)\n", gdt, gccb, timeout));
550 gdt->sc_state |= GDT_POLL_WAIT;
553 if (gdt == gdt_wait_gdt &&
554 gccb->gc_cmd_index == gdt_wait_index) {
560 gdt->sc_state &= ~GDT_POLL_WAIT;
562 while (gdt->sc_test_busy(gdt))
563 DELAY(1); /* XXX correct? */
569 gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
570 u_int8_t service, u_int16_t opcode,
571 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
575 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cmd(%p, %d, %d, %d, %d, %d)\n",
576 gdt, service, opcode, arg1, arg2, arg3));
578 bzero(gdt->sc_cmd, GDT_CMD_SZ);
580 for (retries = GDT_RETRIES; ; ) {
581 gccb->gc_service = service;
582 gccb->gc_flags = GDT_GCF_INTERNAL;
584 gdt->sc_set_sema0(gdt);
585 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
587 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, opcode);
590 case GDT_CACHESERVICE:
591 if (opcode == GDT_IOCTL) {
592 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
593 GDT_IOCTL_SUBFUNC, arg1);
594 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
595 GDT_IOCTL_CHANNEL, arg2);
596 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION +
597 GDT_IOCTL_PARAM_SIZE, (u_int16_t)arg3);
598 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
599 gdt_ccb_vtop(gdt, gccb) +
600 offsetof(struct gdt_ccb, gc_scratch[0]));
602 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION +
603 GDT_CACHE_DEVICENO, (u_int16_t)arg1);
604 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
605 GDT_CACHE_BLOCKNO, arg2);
609 case GDT_SCSIRAWSERVICE:
610 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION +
611 GDT_RAW_DIRECTION, arg1);
612 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
614 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
616 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
617 (u_int8_t)(arg3 >> 8);
620 gdt->sc_cmd_len = GDT_CMD_SZ;
623 gdt->sc_copy_cmd(gdt, gccb);
624 gdt->sc_release_event(gdt);
626 if (!gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT))
628 if (gdt->sc_status != GDT_S_BSY || --retries == 0)
632 return (gdt->sc_status == GDT_S_OK);
635 static struct gdt_ccb *
636 gdt_get_ccb(struct gdt_softc *gdt)
638 struct gdt_ccb *gccb;
641 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_get_ccb(%p)\n", gdt));
644 gccb = SLIST_FIRST(&gdt->sc_free_gccb);
646 SLIST_REMOVE_HEAD(&gdt->sc_free_gccb, sle);
647 SLIST_INSERT_HEAD(&gdt->sc_pending_gccb, gccb, sle);
648 ++gdt_stat.cmd_index_act;
649 if (gdt_stat.cmd_index_act > gdt_stat.cmd_index_max)
650 gdt_stat.cmd_index_max = gdt_stat.cmd_index_act;
657 gdt_free_ccb(struct gdt_softc *gdt, struct gdt_ccb *gccb)
661 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_free_ccb(%p, %p)\n", gdt, gccb));
664 gccb->gc_flags = GDT_GCF_UNUSED;
665 SLIST_REMOVE(&gdt->sc_pending_gccb, gccb, gdt_ccb, sle);
666 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
667 --gdt_stat.cmd_index_act;
669 if (gdt->sc_state & GDT_SHUTDOWN)
674 gdt_ccb_vtop(struct gdt_softc *gdt, struct gdt_ccb *gccb)
676 return (gdt->sc_gccb_busbase
677 + (u_int32_t)((caddr_t)gccb - (caddr_t)gdt->sc_gccbs));
681 gdt_next(struct gdt_softc *gdt)
687 int bus, target, lun;
690 struct ccb_scsiio *csio;
691 struct ccb_hdr *ccbh;
692 struct gdt_ccb *gccb = NULL;
695 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_next(%p)\n", gdt));
698 if (gdt->sc_test_busy(gdt)) {
699 if (!(gdt->sc_state & GDT_POLLING)) {
703 while (gdt->sc_test_busy(gdt))
707 gdt->sc_cmd_cnt = gdt->sc_cmd_off = 0;
710 /* I/Os in queue? controller ready? */
711 if (!TAILQ_FIRST(&gdt->sc_ucmd_queue) &&
712 !TAILQ_FIRST(&gdt->sc_ccb_queue))
715 /* 1.: I/Os without ccb (IOCTLs) */
716 ucmd = TAILQ_FIRST(&gdt->sc_ucmd_queue);
718 TAILQ_REMOVE(&gdt->sc_ucmd_queue, ucmd, links);
719 if ((gccb = gdt_ioctl_cmd(gdt, ucmd, &lock)) == NULL) {
720 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
724 /* wenn mehrere Kdos. zulassen: if (!gdt_polling) continue; */
727 /* 2.: I/Os with ccb */
728 ccb = (union ccb *)TAILQ_FIRST(&gdt->sc_ccb_queue);
729 /* ist dann immer != NULL, da oben getestet */
730 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
731 bus = cam_sim_bus(sim);
732 target = ccb->ccb_h.target_id;
733 lun = ccb->ccb_h.target_lun;
735 TAILQ_REMOVE(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
736 --gdt_stat.req_queue_act;
737 /* ccb->ccb_h.func_code is XPT_SCSI_IO */
738 GDT_DPRINTF(GDT_D_QUEUE, ("XPT_SCSI_IO flags 0x%x)\n",
742 cmd = csio->cdb_io.cdb_bytes[0];
743 /* Max CDB length is 12 bytes */
744 if (csio->cdb_len > 12) {
745 ccbh->status = CAM_REQ_INVALID;
746 --gdt_stat.io_count_act;
748 } else if (bus != gdt->sc_virt_bus) {
749 /* raw service command */
750 if ((gccb = gdt_raw_cmd(gdt, ccb, &lock)) == NULL) {
751 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
753 ++gdt_stat.req_queue_act;
754 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
755 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
758 } else if (target >= GDT_MAX_HDRIVES ||
759 !gdt->sc_hdr[target].hd_present || lun != 0) {
760 ccbh->status = CAM_DEV_NOT_THERE;
761 --gdt_stat.io_count_act;
764 /* cache service command */
765 if (cmd == READ_6 || cmd == WRITE_6 ||
766 cmd == READ_10 || cmd == WRITE_10) {
767 if ((gccb = gdt_cache_cmd(gdt, ccb, &lock)) == NULL) {
768 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
770 ++gdt_stat.req_queue_act;
771 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
772 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
777 gdt_internal_cache_cmd(gdt, ccb);
781 if ((gdt->sc_state & GDT_POLLING) || !next_cmd)
784 if (gdt->sc_cmd_cnt > 0)
785 gdt->sc_release_event(gdt);
789 if ((gdt->sc_state & GDT_POLLING) && gdt->sc_cmd_cnt > 0) {
790 gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT);
794 static struct gdt_ccb *
795 gdt_raw_cmd(struct gdt_softc *gdt, union ccb *ccb, int *lock)
797 struct gdt_ccb *gccb;
800 GDT_DPRINTF(GDT_D_CMD, ("gdt_raw_cmd(%p, %p)\n", gdt, ccb));
802 if (roundup(GDT_CMD_UNION + GDT_RAW_SZ, sizeof(u_int32_t)) +
803 gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
804 gdt->sc_ic_all_size) {
805 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_raw_cmd(): DPMEM overflow\n",
810 bzero(gdt->sc_cmd, GDT_CMD_SZ);
812 gccb = gdt_get_ccb(gdt);
814 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: No free command index found\n",
818 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
820 gccb->gc_service = GDT_SCSIRAWSERVICE;
821 gccb->gc_flags = GDT_GCF_SCSI;
823 if (gdt->sc_cmd_cnt == 0)
824 gdt->sc_set_sema0(gdt);
826 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
828 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
830 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
831 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
832 GDT_DATA_IN : GDT_DATA_OUT);
833 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
834 ccb->csio.dxfer_len);
835 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
837 bcopy(ccb->csio.cdb_io.cdb_bytes, gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
839 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
840 ccb->ccb_h.target_id;
841 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
842 ccb->ccb_h.target_lun;
843 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
845 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
846 sizeof(struct scsi_sense_data));
847 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
848 gdt_ccb_vtop(gdt, gccb) +
849 offsetof(struct gdt_ccb, gc_scratch[0]));
852 * If we have any data to send with this command,
853 * map it into bus space.
855 /* Only use S/G if there is a transfer */
856 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
857 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
858 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
862 /* vorher unlock von splcam() ??? */
865 bus_dmamap_load(gdt->sc_buffer_dmat,
871 if (error == EINPROGRESS) {
872 xpt_freeze_simq(sim, 1);
873 gccb->gc_state |= CAM_RELEASE_SIMQ;
877 panic("iir: CAM_DATA_PHYS not supported");
880 struct bus_dma_segment *segs;
882 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
883 panic("iir%d: iir_action - Physical "
884 "segment pointers unsupported", gdt->sc_hanum);
886 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
887 panic("iir%d: iir_action - Virtual "
888 "segment addresses unsupported", gdt->sc_hanum);
890 /* Just use the segments provided */
891 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
892 gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
895 gdtexecuteccb(gccb, NULL, 0, 0);
902 static struct gdt_ccb *
903 gdt_cache_cmd(struct gdt_softc *gdt, union ccb *ccb, int *lock)
905 struct gdt_ccb *gccb;
909 u_int32_t blockno, blockcnt;
911 GDT_DPRINTF(GDT_D_CMD, ("gdt_cache_cmd(%p, %p)\n", gdt, ccb));
913 if (roundup(GDT_CMD_UNION + GDT_CACHE_SZ, sizeof(u_int32_t)) +
914 gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
915 gdt->sc_ic_all_size) {
916 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_cache_cmd(): DPMEM overflow\n",
921 bzero(gdt->sc_cmd, GDT_CMD_SZ);
923 gccb = gdt_get_ccb(gdt);
925 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
929 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
931 gccb->gc_service = GDT_CACHESERVICE;
932 gccb->gc_flags = GDT_GCF_SCSI;
934 if (gdt->sc_cmd_cnt == 0)
935 gdt->sc_set_sema0(gdt);
937 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
939 cmdp = ccb->csio.cdb_io.cdb_bytes;
940 opcode = (*cmdp == WRITE_6 || *cmdp == WRITE_10) ? GDT_WRITE : GDT_READ;
941 if ((gdt->sc_state & GDT_SHUTDOWN) && opcode == GDT_WRITE)
942 opcode = GDT_WRITE_THR;
943 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, opcode);
945 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
946 ccb->ccb_h.target_id);
947 if (ccb->csio.cdb_len == 6) {
948 struct scsi_rw_6 *rw = (struct scsi_rw_6 *)cmdp;
949 blockno = scsi_3btoul(rw->addr) & ((SRW_TOPADDR<<16) | 0xffff);
950 blockcnt = rw->length ? rw->length : 0x100;
952 struct scsi_rw_10 *rw = (struct scsi_rw_10 *)cmdp;
953 blockno = scsi_4btoul(rw->addr);
954 blockcnt = scsi_2btoul(rw->length);
956 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
958 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
962 * If we have any data to send with this command,
963 * map it into bus space.
965 /* Only use S/G if there is a transfer */
966 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
967 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
971 /* vorher unlock von splcam() ??? */
974 bus_dmamap_load(gdt->sc_buffer_dmat,
980 if (error == EINPROGRESS) {
981 xpt_freeze_simq(sim, 1);
982 gccb->gc_state |= CAM_RELEASE_SIMQ;
986 panic("iir: CAM_DATA_PHYS not supported");
989 struct bus_dma_segment *segs;
991 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
992 panic("iir%d: iir_action - Physical "
993 "segment pointers unsupported", gdt->sc_hanum);
995 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
996 panic("iir%d: iir_action - Virtual "
997 "segment addresses unsupported", gdt->sc_hanum);
999 /* Just use the segments provided */
1000 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
1001 gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
1008 static struct gdt_ccb *
1009 gdt_ioctl_cmd(struct gdt_softc *gdt, gdt_ucmd_t *ucmd, int *lock)
1011 struct gdt_ccb *gccb;
1014 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_ioctl_cmd(%p, %p)\n", gdt, ucmd));
1016 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1018 gccb = gdt_get_ccb(gdt);
1020 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
1024 gccb->gc_ucmd = ucmd;
1025 gccb->gc_service = ucmd->service;
1026 gccb->gc_flags = GDT_GCF_IOCTL;
1028 /* check DPMEM space, copy data buffer from user space */
1029 if (ucmd->service == GDT_CACHESERVICE) {
1030 if (ucmd->OpCode == GDT_IOCTL) {
1031 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_IOCTL_SZ,
1033 cnt = ucmd->u.ioctl.param_size;
1034 if (cnt > GDT_SCRATCH_SZ) {
1035 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1036 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1037 gdt_free_ccb(gdt, gccb);
1041 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1042 GDT_SG_SZ, sizeof(u_int32_t));
1043 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1044 if (cnt > GDT_SCRATCH_SZ) {
1045 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1046 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1047 gdt_free_ccb(gdt, gccb);
1052 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1053 GDT_SG_SZ, sizeof(u_int32_t));
1054 cnt = ucmd->u.raw.sdlen;
1055 if (cnt + ucmd->u.raw.sense_len > GDT_SCRATCH_SZ) {
1056 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1057 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt + ucmd->u.raw.sense_len);
1058 gdt_free_ccb(gdt, gccb);
1063 bcopy(ucmd->data, gccb->gc_scratch, cnt);
1065 if (gdt->sc_cmd_off + gdt->sc_cmd_len + GDT_DPMEM_COMMAND_OFFSET >
1066 gdt->sc_ic_all_size) {
1067 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_ioctl_cmd(): DPMEM overflow\n",
1069 gdt_free_ccb(gdt, gccb);
1073 if (gdt->sc_cmd_cnt == 0)
1074 gdt->sc_set_sema0(gdt);
1077 /* fill cmd structure */
1078 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1079 gccb->gc_cmd_index);
1080 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE,
1083 if (ucmd->service == GDT_CACHESERVICE) {
1084 if (ucmd->OpCode == GDT_IOCTL) {
1086 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_PARAM_SIZE,
1087 ucmd->u.ioctl.param_size);
1088 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_SUBFUNC,
1089 ucmd->u.ioctl.subfunc);
1090 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_CHANNEL,
1091 ucmd->u.ioctl.channel);
1092 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
1093 gdt_ccb_vtop(gdt, gccb) +
1094 offsetof(struct gdt_ccb, gc_scratch[0]));
1096 /* cache service command */
1097 gdt_enc16(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
1098 ucmd->u.cache.DeviceNo);
1099 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
1100 ucmd->u.cache.BlockNo);
1101 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
1102 ucmd->u.cache.BlockCnt);
1103 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1105 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1107 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1108 GDT_SG_PTR, gdt_ccb_vtop(gdt, gccb) +
1109 offsetof(struct gdt_ccb, gc_scratch[0]));
1110 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1111 GDT_SG_LEN, ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE);
1114 /* raw service command */
1115 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
1116 ucmd->u.raw.direction);
1117 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1119 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
1121 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
1123 bcopy(ucmd->u.raw.cmd, gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
1125 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
1127 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
1129 gdt->sc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
1131 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
1132 ucmd->u.raw.sense_len);
1133 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
1134 gdt_ccb_vtop(gdt, gccb) +
1135 offsetof(struct gdt_ccb, gc_scratch[ucmd->u.raw.sdlen]));
1136 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1138 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1139 GDT_SG_PTR, gdt_ccb_vtop(gdt, gccb) +
1140 offsetof(struct gdt_ccb, gc_scratch[0]));
1141 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1142 GDT_SG_LEN, ucmd->u.raw.sdlen);
1146 gdt_stat.sg_count_act = 1;
1147 gdt->sc_copy_cmd(gdt, gccb);
1152 gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb)
1156 t = ccb->ccb_h.target_id;
1157 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cache_cmd(%p, %p, 0x%x, %d)\n",
1158 gdt, ccb, ccb->csio.cdb_io.cdb_bytes[0], t));
1160 switch (ccb->csio.cdb_io.cdb_bytes[0]) {
1161 case TEST_UNIT_READY:
1165 GDT_DPRINTF(GDT_D_MISC, ("REQUEST_SENSE\n"));
1169 struct scsi_inquiry_data *inq;
1171 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
1172 bzero(inq, sizeof(struct scsi_inquiry_data));
1173 inq->device = (gdt->sc_hdr[t].hd_devtype & 4) ?
1175 inq->dev_qual2 = (gdt->sc_hdr[t].hd_devtype & 1) ? 0x80 : 0;
1176 inq->version = SCSI_REV_2;
1177 inq->response_format = 2;
1178 inq->additional_length = 32;
1179 inq->flags = SID_CmdQue | SID_Sync;
1180 strcpy(inq->vendor, gdt->oem_name);
1181 sprintf(inq->product, "Host Drive #%02d", t);
1182 strcpy(inq->revision, " ");
1188 struct scsi_mode_hdr_6 hd;
1189 struct scsi_mode_block_descr bd;
1190 struct scsi_control_page cp;
1194 mpd = (struct mpd_data *)ccb->csio.data_ptr;
1195 bzero(mpd, sizeof(struct mpd_data));
1196 mpd->hd.datalen = sizeof(struct scsi_mode_hdr_6) +
1197 sizeof(struct scsi_mode_block_descr);
1198 mpd->hd.dev_specific = (gdt->sc_hdr[t].hd_devtype & 2) ? 0x80 : 0;
1199 mpd->hd.block_descr_len = sizeof(struct scsi_mode_block_descr);
1200 mpd->bd.block_len[0] = (GDT_SECTOR_SIZE & 0x00ff0000) >> 16;
1201 mpd->bd.block_len[1] = (GDT_SECTOR_SIZE & 0x0000ff00) >> 8;
1202 mpd->bd.block_len[2] = (GDT_SECTOR_SIZE & 0x000000ff);
1203 page=((struct scsi_mode_sense_6 *)ccb->csio.cdb_io.cdb_bytes)->page;
1206 GDT_DPRINTF(GDT_D_MISC, ("MODE_SENSE_6: page 0x%x\n", page));
1213 struct scsi_read_capacity_data *rcd;
1215 rcd = (struct scsi_read_capacity_data *)ccb->csio.data_ptr;
1216 bzero(rcd, sizeof(struct scsi_read_capacity_data));
1217 scsi_ulto4b(gdt->sc_hdr[t].hd_size - 1, rcd->addr);
1218 scsi_ulto4b(GDT_SECTOR_SIZE, rcd->length);
1222 GDT_DPRINTF(GDT_D_MISC, ("gdt_internal_cache_cmd(%d) unknown\n",
1223 ccb->csio.cdb_io.cdb_bytes[0]));
1226 ccb->ccb_h.status = CAM_REQ_CMP;
1227 --gdt_stat.io_count_act;
1232 gdtmapmem(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1234 bus_addr_t *busaddrp;
1236 busaddrp = (bus_addr_t *)arg;
1237 *busaddrp = dm_segs->ds_addr;
1241 gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1243 struct gdt_ccb *gccb;
1245 struct gdt_softc *gdt;
1250 gccb = (struct gdt_ccb *)arg;
1252 gdt = cam_sim_softc((struct cam_sim *)ccb->ccb_h.ccb_sim_ptr);
1254 GDT_DPRINTF(GDT_D_CMD, ("gdtexecuteccb(%p, %p, %p, %d, %d)\n",
1255 gdt, gccb, dm_segs, nseg, error));
1256 gdt_stat.sg_count_act = nseg;
1257 if (nseg > gdt_stat.sg_count_max)
1258 gdt_stat.sg_count_max = nseg;
1260 /* Copy the segments into our SG list */
1261 if (gccb->gc_service == GDT_CACHESERVICE) {
1262 for (i = 0; i < nseg; ++i) {
1263 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1264 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1265 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1266 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1269 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1271 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1274 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1275 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1277 for (i = 0; i < nseg; ++i) {
1278 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1279 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1280 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1281 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1284 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1286 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1289 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1290 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1294 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap,
1295 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
1296 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
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 ccb->ccb_h.timeout_ch =
1306 timeout(iir_timeout, (caddr_t)gccb,
1307 (ccb->ccb_h.timeout * hz) / 1000);
1309 gdt->sc_copy_cmd(gdt, gccb);
1315 iir_action( struct cam_sim *sim, union ccb *ccb )
1317 struct gdt_softc *gdt;
1318 int lock, bus, target, lun;
1320 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1321 ccb->ccb_h.ccb_sim_ptr = sim;
1322 bus = cam_sim_bus(sim);
1323 target = ccb->ccb_h.target_id;
1324 lun = ccb->ccb_h.target_lun;
1325 GDT_DPRINTF(GDT_D_CMD,
1326 ("iir_action(%p) func 0x%x cmd 0x%x bus %d target %d lun %d\n",
1327 gdt, ccb->ccb_h.func_code, ccb->csio.cdb_io.cdb_bytes[0],
1329 ++gdt_stat.io_count_act;
1330 if (gdt_stat.io_count_act > gdt_stat.io_count_max)
1331 gdt_stat.io_count_max = gdt_stat.io_count_act;
1333 switch (ccb->ccb_h.func_code) {
1336 TAILQ_INSERT_TAIL(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1337 ++gdt_stat.req_queue_act;
1338 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1339 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1343 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1344 case XPT_ABORT: /* Abort the specified CCB */
1346 ccb->ccb_h.status = CAM_REQ_INVALID;
1347 --gdt_stat.io_count_act;
1350 case XPT_SET_TRAN_SETTINGS:
1351 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1352 --gdt_stat.io_count_act;
1355 case XPT_GET_TRAN_SETTINGS:
1356 /* Get default/user set transfer settings for the target */
1358 struct ccb_trans_settings *cts;
1362 target_mask = 0x01 << target;
1363 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1364 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
1365 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1366 cts->sync_period = 25; /* 10MHz */
1367 if (cts->sync_period != 0)
1368 cts->sync_offset = 15;
1370 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1371 | CCB_TRANS_SYNC_OFFSET_VALID
1372 | CCB_TRANS_BUS_WIDTH_VALID
1373 | CCB_TRANS_DISC_VALID
1374 | CCB_TRANS_TQ_VALID;
1375 ccb->ccb_h.status = CAM_REQ_CMP;
1377 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1379 --gdt_stat.io_count_act;
1383 case XPT_CALC_GEOMETRY:
1385 struct ccb_calc_geometry *ccg;
1386 u_int32_t secs_per_cylinder;
1389 ccg->heads = gdt->sc_hdr[target].hd_heads;
1390 ccg->secs_per_track = gdt->sc_hdr[target].hd_secs;
1391 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1392 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1393 ccb->ccb_h.status = CAM_REQ_CMP;
1394 --gdt_stat.io_count_act;
1398 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1401 ccb->ccb_h.status = CAM_REQ_CMP;
1402 --gdt_stat.io_count_act;
1406 case XPT_TERM_IO: /* Terminate the I/O process */
1408 ccb->ccb_h.status = CAM_REQ_INVALID;
1409 --gdt_stat.io_count_act;
1412 case XPT_PATH_INQ: /* Path routing inquiry */
1414 struct ccb_pathinq *cpi = &ccb->cpi;
1416 cpi->version_num = 1;
1417 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1418 cpi->hba_inquiry |= PI_WIDE_16;
1419 cpi->target_sprt = 1;
1421 cpi->hba_eng_cnt = 0;
1422 if (bus == gdt->sc_virt_bus)
1423 cpi->max_target = GDT_MAX_HDRIVES - 1;
1424 else if (gdt->sc_class & GDT_FC)
1425 cpi->max_target = GDT_MAXID_FC - 1;
1427 cpi->max_target = GDT_MAXID - 1;
1429 cpi->unit_number = cam_sim_unit(sim);
1432 (bus == gdt->sc_virt_bus ? 127 : gdt->sc_bus_id[bus]);
1433 cpi->base_transfer_speed = 3300;
1434 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1435 if (gdt->sc_vendor == INTEL_VENDOR_ID)
1436 strncpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN);
1438 strncpy(cpi->hba_vid, "ICP vortex ", HBA_IDLEN);
1439 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1440 cpi->ccb_h.status = CAM_REQ_CMP;
1441 --gdt_stat.io_count_act;
1446 GDT_DPRINTF(GDT_D_INVALID, ("gdt_next(%p) cmd 0x%x invalid\n",
1447 gdt, ccb->ccb_h.func_code));
1448 ccb->ccb_h.status = CAM_REQ_INVALID;
1449 --gdt_stat.io_count_act;
1456 iir_poll( struct cam_sim *sim )
1458 struct gdt_softc *gdt;
1460 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1461 GDT_DPRINTF(GDT_D_CMD, ("iir_poll sim %p gdt %p\n", sim, gdt));
1466 iir_timeout(void *arg)
1468 GDT_DPRINTF(GDT_D_TIMEOUT, ("iir_timeout(%p)\n", arg));
1472 iir_watchdog(void *arg)
1474 struct gdt_softc *gdt;
1476 gdt = (struct gdt_softc *)arg;
1477 GDT_DPRINTF(GDT_D_DEBUG, ("iir_watchdog(%p)\n", gdt));
1480 int ccbs = 0, ucmds = 0, frees = 0, pends = 0;
1485 for (h = TAILQ_FIRST(&gdt->sc_ccb_queue); h != NULL;
1486 h = TAILQ_NEXT(h, sim_links.tqe))
1488 for (u = TAILQ_FIRST(&gdt->sc_ucmd_queue); u != NULL;
1489 u = TAILQ_NEXT(u, links))
1491 for (p = SLIST_FIRST(&gdt->sc_free_gccb); p != NULL;
1492 p = SLIST_NEXT(p, sle))
1494 for (p = SLIST_FIRST(&gdt->sc_pending_gccb); p != NULL;
1495 p = SLIST_NEXT(p, sle))
1498 GDT_DPRINTF(GDT_D_TIMEOUT, ("ccbs %d ucmds %d frees %d pends %d\n",
1499 ccbs, ucmds, frees, pends));
1502 timeout(iir_watchdog, (caddr_t)gdt, hz * 15);
1506 iir_shutdown( void *arg, int howto )
1508 struct gdt_softc *gdt;
1509 struct gdt_ccb *gccb;
1513 gdt = (struct gdt_softc *)arg;
1514 GDT_DPRINTF(GDT_D_CMD, ("iir_shutdown(%p, %d)\n", gdt, howto));
1516 printf("iir%d: Flushing all Host Drives. Please wait ... ",
1519 /* allocate ucmd buffer */
1520 ucmd = malloc(sizeof(gdt_ucmd_t), M_DEVBUF, M_NOWAIT);
1522 printf("iir%d: iir_shutdown(): Cannot allocate resource\n",
1526 bzero(ucmd, sizeof(gdt_ucmd_t));
1528 /* wait for pending IOs */
1530 gdt->sc_state = GDT_SHUTDOWN;
1532 if ((gccb = SLIST_FIRST(&gdt->sc_pending_gccb)) != NULL)
1533 (void) tsleep((void *)gccb, PCATCH | PRIBIO, "iirshw", 100 * hz);
1536 for (i = 0; i < GDT_MAX_HDRIVES; ++i) {
1537 if (gdt->sc_hdr[i].hd_present) {
1538 ucmd->service = GDT_CACHESERVICE;
1539 ucmd->OpCode = GDT_FLUSH;
1540 ucmd->u.cache.DeviceNo = i;
1542 TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
1543 ucmd->complete_flag = FALSE;
1546 if (!ucmd->complete_flag)
1547 (void) tsleep((void *)ucmd, PCATCH|PRIBIO, "iirshw", 10*hz);
1551 free(ucmd, M_DEVBUF);
1558 struct gdt_softc *gdt = arg;
1559 struct gdt_intr_ctx ctx;
1561 struct gdt_ccb *gccb;
1565 GDT_DPRINTF(GDT_D_INTR, ("gdt_intr(%p)\n", gdt));
1567 /* If polling and we were not called from gdt_wait, just return */
1568 if ((gdt->sc_state & GDT_POLLING) &&
1569 !(gdt->sc_state & GDT_POLL_WAIT))
1572 if (!(gdt->sc_state & GDT_POLLING))
1576 ctx.istatus = gdt->sc_get_status(gdt);
1578 if (!(gdt->sc_state & GDT_POLLING))
1580 gdt->sc_status = GDT_S_NO_STATUS;
1584 gdt->sc_intr(gdt, &ctx);
1586 gdt->sc_status = ctx.cmd_status;
1587 gdt->sc_service = ctx.service;
1588 gdt->sc_info = ctx.info;
1589 gdt->sc_info2 = ctx.info2;
1591 if (gdt->sc_state & GDT_POLL_WAIT) {
1593 gdt_wait_index = ctx.istatus;
1596 if (ctx.istatus == GDT_ASYNCINDEX) {
1597 gdt_async_event(gdt, ctx.service);
1598 if (!(gdt->sc_state & GDT_POLLING))
1602 if (ctx.istatus == GDT_SPEZINDEX) {
1603 GDT_DPRINTF(GDT_D_INVALID,
1604 ("iir%d: Service unknown or not initialized!\n",
1606 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1607 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1608 gdt_store_event(GDT_ES_DRIVER, 4, &gdt->sc_dvr);
1609 if (!(gdt->sc_state & GDT_POLLING))
1614 gccb = &gdt->sc_gccbs[ctx.istatus - 2];
1615 ctx.service = gccb->gc_service;
1617 switch (gccb->gc_flags) {
1618 case GDT_GCF_UNUSED:
1619 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: Index (%d) to unused command!\n",
1620 gdt->sc_hanum, ctx.istatus));
1621 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1622 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1623 gdt->sc_dvr.eu.driver.index = ctx.istatus;
1624 gdt_store_event(GDT_ES_DRIVER, 1, &gdt->sc_dvr);
1625 gdt_free_ccb(gdt, gccb);
1628 case GDT_GCF_INTERNAL:
1629 if (!(gdt->sc_state & GDT_POLLING))
1634 ucmd = gccb->gc_ucmd;
1635 if (gdt->sc_status == GDT_S_BSY) {
1636 GDT_DPRINTF(GDT_D_DEBUG, ("iir_intr(%p) ioctl: gccb %p busy\n",
1638 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
1639 if (!(gdt->sc_state & GDT_POLLING))
1642 ucmd->status = gdt->sc_status;
1643 ucmd->info = gdt->sc_info;
1644 ucmd->complete_flag = TRUE;
1645 if (ucmd->service == GDT_CACHESERVICE) {
1646 if (ucmd->OpCode == GDT_IOCTL) {
1647 cnt = ucmd->u.ioctl.param_size;
1649 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1651 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1653 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1656 cnt = ucmd->u.raw.sdlen;
1658 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1659 if (ucmd->u.raw.sense_len != 0)
1660 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1662 gdt_free_ccb(gdt, gccb);
1663 if (!(gdt->sc_state & GDT_POLLING))
1672 gdt_free_ccb(gdt, gccb);
1673 gdt_sync_event(gdt, ctx.service, ctx.istatus, gccb);
1674 if (!(gdt->sc_state & GDT_POLLING))
1682 gdt_async_event(struct gdt_softc *gdt, int service)
1684 struct gdt_ccb *gccb;
1686 GDT_DPRINTF(GDT_D_INTR, ("gdt_async_event(%p, %d)\n", gdt, service));
1688 if (service == GDT_SCREENSERVICE) {
1689 if (gdt->sc_status == GDT_MSG_REQUEST) {
1690 while (gdt->sc_test_busy(gdt))
1692 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1693 gccb = gdt_get_ccb(gdt);
1695 printf("iir%d: No free command index found\n",
1699 gccb->gc_service = service;
1700 gccb->gc_flags = GDT_GCF_SCREEN;
1701 gdt->sc_set_sema0(gdt);
1702 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1703 gccb->gc_cmd_index);
1704 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_READ);
1705 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1706 GDT_MSG_INV_HANDLE);
1707 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1708 gdt_ccb_vtop(gdt, gccb) +
1709 offsetof(struct gdt_ccb, gc_scratch[0]));
1710 gdt->sc_cmd_off = 0;
1711 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1713 gdt->sc_cmd_cnt = 0;
1714 gdt->sc_copy_cmd(gdt, gccb);
1715 printf("iir%d: [PCI %d/%d] ",
1716 gdt->sc_hanum,gdt->sc_bus,gdt->sc_slot);
1717 gdt->sc_release_event(gdt);
1721 if ((gdt->sc_fw_vers & 0xff) >= 0x1a) {
1722 gdt->sc_dvr.size = 0;
1723 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1724 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1725 /* severity and event_string already set! */
1727 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.async);
1728 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1729 gdt->sc_dvr.eu.async.service = service;
1730 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1731 gdt->sc_dvr.eu.async.info = gdt->sc_info;
1732 *(u_int32_t *)gdt->sc_dvr.eu.async.scsi_coord = gdt->sc_info2;
1734 gdt_store_event(GDT_ES_ASYNC, service, &gdt->sc_dvr);
1735 printf("iir%d: %s\n", gdt->sc_hanum, gdt->sc_dvr.event_string);
1742 gdt_sync_event(struct gdt_softc *gdt, int service,
1743 u_int8_t index, struct gdt_ccb *gccb)
1747 GDT_DPRINTF(GDT_D_INTR,
1748 ("gdt_sync_event(%p, %d, %d, %p)\n", gdt,service,index,gccb));
1752 if (service == GDT_SCREENSERVICE) {
1755 msg_len = gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_LEN);
1757 if (!(gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1758 gccb->gc_scratch[GDT_SCR_MSG_EXT])) {
1759 gccb->gc_scratch[GDT_SCR_MSG_TEXT + msg_len] = '\0';
1760 printf("%s",&gccb->gc_scratch[GDT_SCR_MSG_TEXT]);
1763 if (gccb->gc_scratch[GDT_SCR_MSG_EXT] &&
1764 !gccb->gc_scratch[GDT_SCR_MSG_ANSWER]) {
1765 while (gdt->sc_test_busy(gdt))
1767 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1768 gccb = gdt_get_ccb(gdt);
1770 printf("iir%d: No free command index found\n",
1774 gccb->gc_service = service;
1775 gccb->gc_flags = GDT_GCF_SCREEN;
1776 gdt->sc_set_sema0(gdt);
1777 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1778 gccb->gc_cmd_index);
1779 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_READ);
1780 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1781 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1782 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1783 gdt_ccb_vtop(gdt, gccb) +
1784 offsetof(struct gdt_ccb, gc_scratch[0]));
1785 gdt->sc_cmd_off = 0;
1786 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1788 gdt->sc_cmd_cnt = 0;
1789 gdt->sc_copy_cmd(gdt, gccb);
1790 gdt->sc_release_event(gdt);
1794 if (gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1795 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN)) {
1796 /* default answers (getchar() not possible) */
1797 if (gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) == 1) {
1798 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN, 0);
1799 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 1);
1800 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 0;
1802 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN,
1803 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) - 2);
1804 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 2);
1805 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 1;
1806 gccb->gc_scratch[GDT_SCR_MSG_TEXT + 1] = 0;
1808 gccb->gc_scratch[GDT_SCR_MSG_EXT] = 0;
1809 gccb->gc_scratch[GDT_SCR_MSG_ANSWER] = 0;
1810 while (gdt->sc_test_busy(gdt))
1812 bzero(gdt->sc_cmd, GDT_CMD_SZ);
1813 gccb = gdt_get_ccb(gdt);
1815 printf("iir%d: No free command index found\n",
1819 gccb->gc_service = service;
1820 gccb->gc_flags = GDT_GCF_SCREEN;
1821 gdt->sc_set_sema0(gdt);
1822 gdt_enc32(gdt->sc_cmd + GDT_CMD_COMMANDINDEX,
1823 gccb->gc_cmd_index);
1824 gdt_enc16(gdt->sc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
1825 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1826 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1827 gdt_enc32(gdt->sc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1828 gdt_ccb_vtop(gdt, gccb) +
1829 offsetof(struct gdt_ccb, gc_scratch[0]));
1830 gdt->sc_cmd_off = 0;
1831 gdt->sc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1833 gdt->sc_cmd_cnt = 0;
1834 gdt->sc_copy_cmd(gdt, gccb);
1835 gdt->sc_release_event(gdt);
1841 untimeout(iir_timeout, gccb, ccb->ccb_h.timeout_ch);
1842 if (gdt->sc_status == GDT_S_BSY) {
1843 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_sync_event(%p) gccb %p busy\n",
1845 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1846 ++gdt_stat.req_queue_act;
1847 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1848 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1852 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap,
1853 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
1854 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1856 ccb->csio.resid = 0;
1857 if (gdt->sc_status == GDT_S_OK) {
1858 ccb->ccb_h.status = CAM_REQ_CMP;
1861 if (gccb->gc_service == GDT_CACHESERVICE) {
1862 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1863 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1864 bzero(&ccb->csio.sense_data, ccb->csio.sense_len);
1865 ccb->csio.sense_data.error_code =
1866 SSD_CURRENT_ERROR | SSD_ERRCODE_VALID;
1867 ccb->csio.sense_data.flags = SSD_KEY_NOT_READY;
1869 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.sync);
1870 gdt->sc_dvr.eu.sync.ionode = gdt->sc_hanum;
1871 gdt->sc_dvr.eu.sync.service = service;
1872 gdt->sc_dvr.eu.sync.status = gdt->sc_status;
1873 gdt->sc_dvr.eu.sync.info = gdt->sc_info;
1874 gdt->sc_dvr.eu.sync.hostdrive = ccb->ccb_h.target_id;
1875 if (gdt->sc_status >= 0x8000)
1876 gdt_store_event(GDT_ES_SYNC, 0, &gdt->sc_dvr);
1878 gdt_store_event(GDT_ES_SYNC, service, &gdt->sc_dvr);
1881 if (gdt->sc_status != GDT_S_RAW_SCSI || gdt->sc_info >= 0x100) {
1882 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1884 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
1885 ccb->csio.scsi_status = gdt->sc_info;
1886 bcopy(gccb->gc_scratch, &ccb->csio.sense_data,
1887 ccb->csio.sense_len);
1891 --gdt_stat.io_count_act;
1897 /* Controller event handling functions */
1898 gdt_evt_str *gdt_store_event(u_int16_t source, u_int16_t idx,
1904 GDT_DPRINTF(GDT_D_MISC, ("gdt_store_event(%d, %d)\n", source, idx));
1905 if (source == 0) /* no source -> no event */
1908 if (ebuffer[elastidx].event_source == source &&
1909 ebuffer[elastidx].event_idx == idx &&
1910 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
1911 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
1912 (char *)&evt->eu, evt->size)) ||
1913 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
1914 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
1915 (char *)&evt->event_string)))) {
1916 e = &ebuffer[elastidx];
1918 e->last_stamp = tv.tv_sec;
1921 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
1923 if (elastidx == GDT_MAX_EVENTS)
1925 if (elastidx == eoldidx) { /* reached mark ? */
1927 if (eoldidx == GDT_MAX_EVENTS)
1931 e = &ebuffer[elastidx];
1932 e->event_source = source;
1935 e->first_stamp = e->last_stamp = tv.tv_sec;
1937 e->event_data = *evt;
1943 int gdt_read_event(int handle, gdt_evt_str *estr)
1948 GDT_DPRINTF(GDT_D_MISC, ("gdt_read_event(%d)\n", handle));
1954 estr->event_source = 0;
1956 if (eindex >= GDT_MAX_EVENTS) {
1960 e = &ebuffer[eindex];
1961 if (e->event_source != 0) {
1962 if (eindex != elastidx) {
1963 if (++eindex == GDT_MAX_EVENTS)
1968 memcpy(estr, e, sizeof(gdt_evt_str));
1974 void gdt_readapp_event(u_int8_t application, gdt_evt_str *estr)
1980 GDT_DPRINTF(GDT_D_MISC, ("gdt_readapp_event(%d)\n", application));
1984 e = &ebuffer[eindex];
1985 if (e->event_source == 0)
1987 if ((e->application & application) == 0) {
1988 e->application |= application;
1992 if (eindex == elastidx)
1994 if (++eindex == GDT_MAX_EVENTS)
1998 memcpy(estr, e, sizeof(gdt_evt_str));
2000 estr->event_source = 0;
2004 void gdt_clear_events()
2006 GDT_DPRINTF(GDT_D_MISC, ("gdt_clear_events\n"));
2008 eoldidx = elastidx = 0;
2009 ebuffer[0].event_source = 0;