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.h>
60 #include <machine/stdarg.h>
63 #include <cam/cam_ccb.h>
64 #include <cam/cam_sim.h>
65 #include <cam/cam_xpt_sim.h>
66 #include <cam/cam_debug.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
73 #include <dev/iir/iir.h>
75 MALLOC_DEFINE(M_GDTBUF, "iirbuf", "iir driver buffer");
77 struct gdt_softc *gdt_wait_gdt;
81 int gdt_debug = GDT_DEBUG;
83 #define MAX_SERBUF 160
84 static void ser_init(void);
85 static void ser_puts(char *str);
86 static void ser_putc(int c);
87 static char strbuf[MAX_SERBUF+1];
89 #define COM_BASE 0x2f8
91 #define COM_BASE 0x3f8
93 static void ser_init()
95 unsigned port=COM_BASE;
99 /* 19200 Baud, if 9600: outb(12,port) */
105 static void ser_puts(char *str)
110 for (ptr=str;*ptr;++ptr)
111 ser_putc((int)(*ptr));
114 static void ser_putc(int c)
116 unsigned port=COM_BASE;
118 while ((inb(port+5) & 0x20)==0);
122 while ((inb(port+5) & 0x20)==0);
127 int ser_printf(const char *fmt, ...)
133 i = vsprintf(strbuf,fmt,args);
141 /* The linked list of softc structures */
142 struct gdt_softc_list gdt_softcs = TAILQ_HEAD_INITIALIZER(gdt_softcs);
143 /* controller cnt. */
146 static gdt_evt_str ebuffer[GDT_MAX_EVENTS];
147 static int elastidx, eoldidx;
149 gdt_statist_t gdt_stat;
151 /* Definitions for our use of the SIM private CCB area */
152 #define ccb_sim_ptr spriv_ptr0
153 #define ccb_priority spriv_field1
155 static void iir_action(struct cam_sim *sim, union ccb *ccb);
156 static void iir_poll(struct cam_sim *sim);
157 static void iir_shutdown(void *arg, int howto);
158 static void iir_timeout(void *arg);
159 static void iir_watchdog(void *arg);
161 static void gdt_eval_mapping(u_int32_t size, int *cyls, int *heads,
163 static int gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
164 u_int8_t service, u_int16_t opcode,
165 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3);
166 static int gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *ccb,
169 static struct gdt_ccb *gdt_get_ccb(struct gdt_softc *gdt);
171 static int gdt_sync_event(struct gdt_softc *gdt, int service,
172 u_int8_t index, struct gdt_ccb *gccb);
173 static int gdt_async_event(struct gdt_softc *gdt, int service);
174 static struct gdt_ccb *gdt_raw_cmd(struct gdt_softc *gdt,
175 union ccb *ccb, int *lock);
176 static struct gdt_ccb *gdt_cache_cmd(struct gdt_softc *gdt,
177 union ccb *ccb, int *lock);
178 static struct gdt_ccb *gdt_ioctl_cmd(struct gdt_softc *gdt,
179 gdt_ucmd_t *ucmd, int *lock);
180 static void gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb);
182 static void gdtmapmem(void *arg, bus_dma_segment_t *dm_segs,
183 int nseg, int error);
184 static void gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
185 int nseg, int error);
188 iir_init(struct gdt_softc *gdt)
191 int i, id, drv_cyls, drv_hds, drv_secs;
192 struct gdt_ccb *gccb;
194 GDT_DPRINTF(GDT_D_DEBUG, ("iir_init()\n"));
196 gdt->sc_state = GDT_POLLING;
198 bzero(&gdt_stat, sizeof(gdt_statist_t));
200 SLIST_INIT(&gdt->sc_free_gccb);
201 SLIST_INIT(&gdt->sc_pending_gccb);
202 TAILQ_INIT(&gdt->sc_ccb_queue);
203 TAILQ_INIT(&gdt->sc_ucmd_queue);
204 TAILQ_INSERT_TAIL(&gdt_softcs, gdt, links);
206 /* DMA tag for mapping buffers into device visible space. */
207 if (bus_dma_tag_create(gdt->sc_parent_dmat, /*alignment*/1, /*boundary*/0,
208 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
209 /*highaddr*/BUS_SPACE_MAXADDR,
210 /*filter*/NULL, /*filterarg*/NULL,
211 /*maxsize*/MAXBSIZE, /*nsegments*/GDT_MAXSG,
212 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
213 /*flags*/BUS_DMA_ALLOCNOW,
214 /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
215 &gdt->sc_buffer_dmat) != 0) {
216 printf("iir%d: bus_dma_tag_create(...,gdt->sc_buffer_dmat) failed\n",
220 gdt->sc_init_level++;
222 /* DMA tag for our ccb structures */
223 if (bus_dma_tag_create(gdt->sc_parent_dmat,
226 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
227 /*highaddr*/BUS_SPACE_MAXADDR,
230 GDT_MAXCMDS * GDT_SCRATCH_SZ, /* maxsize */
232 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
233 /*flags*/0, /*lockfunc*/busdma_lock_mutex,
234 /*lockarg*/&Giant, &gdt->sc_gcscratch_dmat) != 0) {
235 printf("iir%d: bus_dma_tag_create(...,gdt->sc_gcscratch_dmat) failed\n",
239 gdt->sc_init_level++;
241 /* Allocation for our ccb scratch area */
242 if (bus_dmamem_alloc(gdt->sc_gcscratch_dmat, (void **)&gdt->sc_gcscratch,
243 BUS_DMA_NOWAIT, &gdt->sc_gcscratch_dmamap) != 0) {
244 printf("iir%d: bus_dmamem_alloc(...,&gdt->sc_gccbs,...) failed\n",
248 gdt->sc_init_level++;
250 /* And permanently map them */
251 bus_dmamap_load(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch_dmamap,
252 gdt->sc_gcscratch, GDT_MAXCMDS * GDT_SCRATCH_SZ,
253 gdtmapmem, &gdt->sc_gcscratch_busbase, /*flags*/0);
254 gdt->sc_init_level++;
256 /* Clear them out. */
257 bzero(gdt->sc_gcscratch, GDT_MAXCMDS * GDT_SCRATCH_SZ);
259 /* Initialize the ccbs */
260 gdt->sc_gccbs = malloc(sizeof(struct gdt_ccb) * GDT_MAXCMDS, M_GDTBUF,
262 if (gdt->sc_gccbs == NULL) {
263 printf("iir%d: no memory for gccbs.\n", gdt->sc_hanum);
266 for (i = GDT_MAXCMDS-1; i >= 0; i--) {
267 gccb = &gdt->sc_gccbs[i];
268 gccb->gc_cmd_index = i + 2;
269 gccb->gc_flags = GDT_GCF_UNUSED;
270 gccb->gc_map_flag = FALSE;
271 if (bus_dmamap_create(gdt->sc_buffer_dmat, /*flags*/0,
272 &gccb->gc_dmamap) != 0)
274 gccb->gc_map_flag = TRUE;
275 gccb->gc_scratch = &gdt->sc_gcscratch[GDT_SCRATCH_SZ * i];
276 gccb->gc_scratch_busbase = gdt->sc_gcscratch_busbase + GDT_SCRATCH_SZ * i;
277 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
279 gdt->sc_init_level++;
281 /* create the control device */
282 gdt->sc_dev = gdt_make_dev(gdt->sc_hanum);
284 /* allocate ccb for gdt_internal_cmd() */
285 gccb = gdt_get_ccb(gdt);
287 printf("iir%d: No free command index found\n",
291 bzero(gccb->gc_cmd, GDT_CMD_SZ);
293 if (!gdt_internal_cmd(gdt, gccb, GDT_SCREENSERVICE, GDT_INIT,
295 printf("iir%d: Screen service initialization error %d\n",
296 gdt->sc_hanum, gdt->sc_status);
297 gdt_free_ccb(gdt, gccb);
301 gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_UNFREEZE_IO,
304 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INIT,
305 GDT_LINUX_OS, 0, 0)) {
306 printf("iir%d: Cache service initialization error %d\n",
307 gdt->sc_hanum, gdt->sc_status);
308 gdt_free_ccb(gdt, gccb);
311 cdev_cnt = (u_int16_t)gdt->sc_info;
312 gdt->sc_fw_vers = gdt->sc_service;
314 /* Detect number of buses */
315 gdt_enc32(gccb->gc_scratch + GDT_IOC_VERSION, GDT_IOC_NEWEST);
316 gccb->gc_scratch[GDT_IOC_LIST_ENTRIES] = GDT_MAXBUS;
317 gccb->gc_scratch[GDT_IOC_FIRST_CHAN] = 0;
318 gccb->gc_scratch[GDT_IOC_LAST_CHAN] = GDT_MAXBUS - 1;
319 gdt_enc32(gccb->gc_scratch + GDT_IOC_LIST_OFFSET, GDT_IOC_HDR_SZ);
320 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
321 GDT_IOCHAN_RAW_DESC, GDT_INVALID_CHANNEL,
322 GDT_IOC_HDR_SZ + GDT_MAXBUS * GDT_RAWIOC_SZ)) {
323 gdt->sc_bus_cnt = gccb->gc_scratch[GDT_IOC_CHAN_COUNT];
324 for (i = 0; i < gdt->sc_bus_cnt; i++) {
325 id = gccb->gc_scratch[GDT_IOC_HDR_SZ +
326 i * GDT_RAWIOC_SZ + GDT_RAWIOC_PROC_ID];
327 gdt->sc_bus_id[i] = id < GDT_MAXID_FC ? id : 0xff;
330 /* New method failed, use fallback. */
331 for (i = 0; i < GDT_MAXBUS; i++) {
332 gdt_enc32(gccb->gc_scratch + GDT_GETCH_CHANNEL_NO, i);
333 if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
334 GDT_SCSI_CHAN_CNT | GDT_L_CTRL_PATTERN,
335 GDT_IO_CHANNEL | GDT_INVALID_CHANNEL,
338 printf("iir%d: Cannot get channel count, "
339 "error %d\n", gdt->sc_hanum, gdt->sc_status);
340 gdt_free_ccb(gdt, gccb);
346 (gccb->gc_scratch[GDT_GETCH_SIOP_ID] < GDT_MAXID_FC) ?
347 gccb->gc_scratch[GDT_GETCH_SIOP_ID] : 0xff;
351 /* add one "virtual" channel for the host drives */
352 gdt->sc_virt_bus = gdt->sc_bus_cnt;
355 if (!gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_INIT,
357 printf("iir%d: Raw service initialization error %d\n",
358 gdt->sc_hanum, gdt->sc_status);
359 gdt_free_ccb(gdt, gccb);
363 /* Set/get features raw service (scatter/gather) */
364 gdt->sc_raw_feat = 0;
365 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_SET_FEAT,
366 GDT_SCATTER_GATHER, 0, 0)) {
367 if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_GET_FEAT,
369 gdt->sc_raw_feat = gdt->sc_info;
370 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
371 panic("iir%d: Scatter/Gather Raw Service "
372 "required but not supported!\n", gdt->sc_hanum);
373 gdt_free_ccb(gdt, gccb);
379 /* Set/get features cache service (scatter/gather) */
380 gdt->sc_cache_feat = 0;
381 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_SET_FEAT,
382 0, GDT_SCATTER_GATHER, 0)) {
383 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_GET_FEAT,
385 gdt->sc_cache_feat = gdt->sc_info;
386 if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
387 panic("iir%d: Scatter/Gather Cache Service "
388 "required but not supported!\n", gdt->sc_hanum);
389 gdt_free_ccb(gdt, gccb);
396 gdt_enc32(gccb->gc_scratch + GDT_OEM_VERSION, 0x01);
397 gdt_enc32(gccb->gc_scratch + GDT_OEM_BUFSIZE, sizeof(gdt_oem_record_t));
398 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
399 GDT_OEM_STR_RECORD, GDT_INVALID_CHANNEL,
400 sizeof(gdt_oem_str_record_t))) {
401 strncpy(gdt->oem_name, ((gdt_oem_str_record_t *)
402 gccb->gc_scratch)->text.scsi_host_drive_inquiry_vendor_id, 7);
403 gdt->oem_name[7]='\0';
405 /* Old method, based on PCI ID */
406 if (gdt->sc_vendor == INTEL_VENDOR_ID)
407 strcpy(gdt->oem_name,"Intel ");
409 strcpy(gdt->oem_name,"ICP ");
412 /* Scan for cache devices */
413 for (i = 0; i < cdev_cnt && i < GDT_MAX_HDRIVES; i++) {
414 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INFO,
416 gdt->sc_hdr[i].hd_present = 1;
417 gdt->sc_hdr[i].hd_size = gdt->sc_info;
420 * Evaluate mapping (sectors per head, heads per cyl)
422 gdt->sc_hdr[i].hd_size &= ~GDT_SECS32;
423 if (gdt->sc_info2 == 0)
424 gdt_eval_mapping(gdt->sc_hdr[i].hd_size,
425 &drv_cyls, &drv_hds, &drv_secs);
427 drv_hds = gdt->sc_info2 & 0xff;
428 drv_secs = (gdt->sc_info2 >> 8) & 0xff;
429 drv_cyls = gdt->sc_hdr[i].hd_size / drv_hds /
432 gdt->sc_hdr[i].hd_heads = drv_hds;
433 gdt->sc_hdr[i].hd_secs = drv_secs;
435 gdt->sc_hdr[i].hd_size = drv_cyls * drv_hds * drv_secs;
437 if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE,
438 GDT_DEVTYPE, i, 0, 0))
439 gdt->sc_hdr[i].hd_devtype = gdt->sc_info;
443 GDT_DPRINTF(GDT_D_INIT, ("dpmem %x %d-bus %d cache device%s\n",
445 gdt->sc_bus_cnt, cdev_cnt,
446 cdev_cnt == 1 ? "" : "s"));
447 gdt_free_ccb(gdt, gccb);
454 iir_free(struct gdt_softc *gdt)
458 GDT_DPRINTF(GDT_D_INIT, ("iir_free()\n"));
460 switch (gdt->sc_init_level) {
462 gdt_destroy_dev(gdt->sc_dev);
464 for (i = GDT_MAXCMDS-1; i >= 0; i--)
465 if (gdt->sc_gccbs[i].gc_map_flag)
466 bus_dmamap_destroy(gdt->sc_buffer_dmat,
467 gdt->sc_gccbs[i].gc_dmamap);
468 bus_dmamap_unload(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch_dmamap);
469 free(gdt->sc_gccbs, M_GDTBUF);
471 bus_dmamem_free(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch, gdt->sc_gcscratch_dmamap);
473 bus_dma_tag_destroy(gdt->sc_gcscratch_dmat);
475 bus_dma_tag_destroy(gdt->sc_buffer_dmat);
477 bus_dma_tag_destroy(gdt->sc_parent_dmat);
481 TAILQ_REMOVE(&gdt_softcs, gdt, links);
485 iir_attach(struct gdt_softc *gdt)
487 struct cam_devq *devq;
490 GDT_DPRINTF(GDT_D_INIT, ("iir_attach()\n"));
493 * Create the device queue for our SIM.
494 * XXX Throttle this down since the card has problems under load.
496 devq = cam_simq_alloc(32);
500 for (i = 0; i < gdt->sc_bus_cnt; i++) {
502 * Construct our SIM entry
504 gdt->sims[i] = cam_sim_alloc(iir_action, iir_poll, "iir",
505 gdt, gdt->sc_hanum, &Giant,
507 /*tagged*/GDT_MAXCMDS, devq);
508 if (xpt_bus_register(gdt->sims[i], gdt->sc_devnode, i) != CAM_SUCCESS) {
509 cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
513 if (xpt_create_path(&gdt->paths[i], /*periph*/NULL,
514 cam_sim_path(gdt->sims[i]),
516 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
517 xpt_bus_deregister(cam_sim_path(gdt->sims[i]));
518 cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
523 EVENTHANDLER_REGISTER(shutdown_final, iir_shutdown,
524 gdt, SHUTDOWN_PRI_DEFAULT);
525 /* iir_watchdog(gdt); */
526 gdt->sc_state = GDT_NORMAL;
530 gdt_eval_mapping(u_int32_t size, int *cyls, int *heads, int *secs)
532 *cyls = size / GDT_HEADS / GDT_SECS;
533 if (*cyls < GDT_MAXCYLS) {
537 /* Too high for 64 * 32 */
538 *cyls = size / GDT_MEDHEADS / GDT_MEDSECS;
539 if (*cyls < GDT_MAXCYLS) {
540 *heads = GDT_MEDHEADS;
543 /* Too high for 127 * 63 */
544 *cyls = size / GDT_BIGHEADS / GDT_BIGSECS;
545 *heads = GDT_BIGHEADS;
552 gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *gccb,
557 GDT_DPRINTF(GDT_D_INIT,
558 ("gdt_wait(%p, %p, %d)\n", gdt, gccb, timeout));
560 gdt->sc_state |= GDT_POLL_WAIT;
563 if (gdt == gdt_wait_gdt &&
564 gccb->gc_cmd_index == gdt_wait_index) {
570 gdt->sc_state &= ~GDT_POLL_WAIT;
572 while (gdt->sc_test_busy(gdt))
573 DELAY(1); /* XXX correct? */
579 gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
580 u_int8_t service, u_int16_t opcode,
581 u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
585 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cmd(%p, %d, %d, %d, %d, %d)\n",
586 gdt, service, opcode, arg1, arg2, arg3));
588 bzero(gccb->gc_cmd, GDT_CMD_SZ);
590 for (retries = GDT_RETRIES; ; ) {
591 gccb->gc_service = service;
592 gccb->gc_flags = GDT_GCF_INTERNAL;
594 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
596 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, opcode);
599 case GDT_CACHESERVICE:
600 if (opcode == GDT_IOCTL) {
601 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
602 GDT_IOCTL_SUBFUNC, arg1);
603 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
604 GDT_IOCTL_CHANNEL, arg2);
605 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION +
606 GDT_IOCTL_PARAM_SIZE, (u_int16_t)arg3);
607 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
608 gccb->gc_scratch_busbase);
610 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION +
611 GDT_CACHE_DEVICENO, (u_int16_t)arg1);
612 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
613 GDT_CACHE_BLOCKNO, arg2);
617 case GDT_SCSIRAWSERVICE:
618 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
619 GDT_RAW_DIRECTION, arg1);
620 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
622 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
624 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
625 (u_int8_t)(arg3 >> 8);
628 gdt->sc_set_sema0(gdt);
629 gccb->gc_cmd_len = GDT_CMD_SZ;
632 gdt->sc_copy_cmd(gdt, gccb);
633 gdt->sc_release_event(gdt);
635 if (!gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT))
637 if (gdt->sc_status != GDT_S_BSY || --retries == 0)
641 return (gdt->sc_status == GDT_S_OK);
644 static struct gdt_ccb *
645 gdt_get_ccb(struct gdt_softc *gdt)
647 struct gdt_ccb *gccb;
650 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_get_ccb(%p)\n", gdt));
653 gccb = SLIST_FIRST(&gdt->sc_free_gccb);
655 SLIST_REMOVE_HEAD(&gdt->sc_free_gccb, sle);
656 SLIST_INSERT_HEAD(&gdt->sc_pending_gccb, gccb, sle);
657 ++gdt_stat.cmd_index_act;
658 if (gdt_stat.cmd_index_act > gdt_stat.cmd_index_max)
659 gdt_stat.cmd_index_max = gdt_stat.cmd_index_act;
666 gdt_free_ccb(struct gdt_softc *gdt, struct gdt_ccb *gccb)
670 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_free_ccb(%p, %p)\n", gdt, gccb));
673 gccb->gc_flags = GDT_GCF_UNUSED;
674 SLIST_REMOVE(&gdt->sc_pending_gccb, gccb, gdt_ccb, sle);
675 SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
676 --gdt_stat.cmd_index_act;
678 if (gdt->sc_state & GDT_SHUTDOWN)
683 gdt_next(struct gdt_softc *gdt)
689 int bus, target, lun;
692 struct ccb_scsiio *csio;
693 struct ccb_hdr *ccbh;
694 struct gdt_ccb *gccb = NULL;
697 GDT_DPRINTF(GDT_D_QUEUE, ("gdt_next(%p)\n", gdt));
700 if (gdt->sc_test_busy(gdt)) {
701 if (!(gdt->sc_state & GDT_POLLING)) {
705 while (gdt->sc_test_busy(gdt))
709 gdt->sc_cmd_cnt = gdt->sc_cmd_off = 0;
712 /* I/Os in queue? controller ready? */
713 if (!TAILQ_FIRST(&gdt->sc_ucmd_queue) &&
714 !TAILQ_FIRST(&gdt->sc_ccb_queue))
717 /* 1.: I/Os without ccb (IOCTLs) */
718 ucmd = TAILQ_FIRST(&gdt->sc_ucmd_queue);
720 TAILQ_REMOVE(&gdt->sc_ucmd_queue, ucmd, links);
721 if ((gccb = gdt_ioctl_cmd(gdt, ucmd, &lock)) == NULL) {
722 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
726 /* wenn mehrere Kdos. zulassen: if (!gdt_polling) continue; */
729 /* 2.: I/Os with ccb */
730 ccb = (union ccb *)TAILQ_FIRST(&gdt->sc_ccb_queue);
731 /* ist dann immer != NULL, da oben getestet */
732 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
733 bus = cam_sim_bus(sim);
734 target = ccb->ccb_h.target_id;
735 lun = ccb->ccb_h.target_lun;
737 TAILQ_REMOVE(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
738 --gdt_stat.req_queue_act;
739 /* ccb->ccb_h.func_code is XPT_SCSI_IO */
740 GDT_DPRINTF(GDT_D_QUEUE, ("XPT_SCSI_IO flags 0x%x)\n",
744 cmd = csio->cdb_io.cdb_bytes[0];
745 /* Max CDB length is 12 bytes */
746 if (csio->cdb_len > 12) {
747 ccbh->status = CAM_REQ_INVALID;
748 --gdt_stat.io_count_act;
750 } else if (bus != gdt->sc_virt_bus) {
751 /* raw service command */
752 if ((gccb = gdt_raw_cmd(gdt, ccb, &lock)) == NULL) {
753 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
755 ++gdt_stat.req_queue_act;
756 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
757 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
760 } else if (target >= GDT_MAX_HDRIVES ||
761 !gdt->sc_hdr[target].hd_present || lun != 0) {
762 ccbh->status = CAM_DEV_NOT_THERE;
763 --gdt_stat.io_count_act;
766 /* cache service command */
767 if (cmd == READ_6 || cmd == WRITE_6 ||
768 cmd == READ_10 || cmd == WRITE_10) {
769 if ((gccb = gdt_cache_cmd(gdt, ccb, &lock)) == NULL) {
770 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h,
772 ++gdt_stat.req_queue_act;
773 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
774 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
779 gdt_internal_cache_cmd(gdt, ccb);
783 if ((gdt->sc_state & GDT_POLLING) || !next_cmd)
786 if (gdt->sc_cmd_cnt > 0)
787 gdt->sc_release_event(gdt);
791 if ((gdt->sc_state & GDT_POLLING) && gdt->sc_cmd_cnt > 0) {
792 gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT);
796 static struct gdt_ccb *
797 gdt_raw_cmd(struct gdt_softc *gdt, union ccb *ccb, int *lock)
799 struct gdt_ccb *gccb;
802 GDT_DPRINTF(GDT_D_CMD, ("gdt_raw_cmd(%p, %p)\n", gdt, ccb));
804 if (roundup(GDT_CMD_UNION + GDT_RAW_SZ, sizeof(u_int32_t)) +
805 gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
806 gdt->sc_ic_all_size) {
807 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_raw_cmd(): DPMEM overflow\n",
812 gccb = gdt_get_ccb(gdt);
814 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: No free command index found\n",
818 bzero(gccb->gc_cmd, GDT_CMD_SZ);
819 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
821 gccb->gc_service = GDT_SCSIRAWSERVICE;
822 gccb->gc_flags = GDT_GCF_SCSI;
824 if (gdt->sc_cmd_cnt == 0)
825 gdt->sc_set_sema0(gdt);
827 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
829 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
831 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
832 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
833 GDT_DATA_IN : GDT_DATA_OUT);
834 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
835 ccb->csio.dxfer_len);
836 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
838 bcopy(ccb->csio.cdb_io.cdb_bytes, gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
840 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
841 ccb->ccb_h.target_id;
842 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
843 ccb->ccb_h.target_lun;
844 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
846 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
847 sizeof(struct scsi_sense_data));
848 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
849 gccb->gc_scratch_busbase);
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_ccb->ccb_h.status |= 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 gccb = gdt_get_ccb(gdt);
923 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
927 bzero(gccb->gc_cmd, GDT_CMD_SZ);
928 sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
930 gccb->gc_service = GDT_CACHESERVICE;
931 gccb->gc_flags = GDT_GCF_SCSI;
933 if (gdt->sc_cmd_cnt == 0)
934 gdt->sc_set_sema0(gdt);
936 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
938 cmdp = ccb->csio.cdb_io.cdb_bytes;
939 opcode = (*cmdp == WRITE_6 || *cmdp == WRITE_10) ? GDT_WRITE : GDT_READ;
940 if ((gdt->sc_state & GDT_SHUTDOWN) && opcode == GDT_WRITE)
941 opcode = GDT_WRITE_THR;
942 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, opcode);
944 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
945 ccb->ccb_h.target_id);
946 if (ccb->csio.cdb_len == 6) {
947 struct scsi_rw_6 *rw = (struct scsi_rw_6 *)cmdp;
948 blockno = scsi_3btoul(rw->addr) & ((SRW_TOPADDR<<16) | 0xffff);
949 blockcnt = rw->length ? rw->length : 0x100;
951 struct scsi_rw_10 *rw = (struct scsi_rw_10 *)cmdp;
952 blockno = scsi_4btoul(rw->addr);
953 blockcnt = scsi_2btoul(rw->length);
955 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
957 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
961 * If we have any data to send with this command,
962 * map it into bus space.
964 /* Only use S/G if there is a transfer */
965 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
966 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
970 /* vorher unlock von splcam() ??? */
973 bus_dmamap_load(gdt->sc_buffer_dmat,
979 if (error == EINPROGRESS) {
980 xpt_freeze_simq(sim, 1);
981 gccb->gc_ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
985 panic("iir: CAM_DATA_PHYS not supported");
988 struct bus_dma_segment *segs;
990 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
991 panic("iir%d: iir_action - Physical "
992 "segment pointers unsupported", gdt->sc_hanum);
994 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
995 panic("iir%d: iir_action - Virtual "
996 "segment addresses unsupported", gdt->sc_hanum);
998 /* Just use the segments provided */
999 segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
1000 gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
1007 static struct gdt_ccb *
1008 gdt_ioctl_cmd(struct gdt_softc *gdt, gdt_ucmd_t *ucmd, int *lock)
1010 struct gdt_ccb *gccb;
1013 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_ioctl_cmd(%p, %p)\n", gdt, ucmd));
1015 gccb = gdt_get_ccb(gdt);
1017 GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
1021 bzero(gccb->gc_cmd, GDT_CMD_SZ);
1022 gccb->gc_ucmd = ucmd;
1023 gccb->gc_service = ucmd->service;
1024 gccb->gc_flags = GDT_GCF_IOCTL;
1026 /* check DPMEM space, copy data buffer from user space */
1027 if (ucmd->service == GDT_CACHESERVICE) {
1028 if (ucmd->OpCode == GDT_IOCTL) {
1029 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_IOCTL_SZ,
1031 cnt = ucmd->u.ioctl.param_size;
1032 if (cnt > GDT_SCRATCH_SZ) {
1033 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1034 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1035 gdt_free_ccb(gdt, gccb);
1039 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1040 GDT_SG_SZ, sizeof(u_int32_t));
1041 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1042 if (cnt > GDT_SCRATCH_SZ) {
1043 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1044 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
1045 gdt_free_ccb(gdt, gccb);
1050 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1051 GDT_SG_SZ, sizeof(u_int32_t));
1052 cnt = ucmd->u.raw.sdlen;
1053 if (cnt + ucmd->u.raw.sense_len > GDT_SCRATCH_SZ) {
1054 printf("iir%d: Scratch buffer too small (%d/%d)\n",
1055 gdt->sc_hanum, GDT_SCRATCH_SZ, cnt + ucmd->u.raw.sense_len);
1056 gdt_free_ccb(gdt, gccb);
1061 bcopy(ucmd->data, gccb->gc_scratch, cnt);
1063 if (gdt->sc_cmd_off + gccb->gc_cmd_len + GDT_DPMEM_COMMAND_OFFSET >
1064 gdt->sc_ic_all_size) {
1065 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_ioctl_cmd(): DPMEM overflow\n",
1067 gdt_free_ccb(gdt, gccb);
1071 if (gdt->sc_cmd_cnt == 0)
1072 gdt->sc_set_sema0(gdt);
1075 /* fill cmd structure */
1076 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
1077 gccb->gc_cmd_index);
1078 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE,
1081 if (ucmd->service == GDT_CACHESERVICE) {
1082 if (ucmd->OpCode == GDT_IOCTL) {
1084 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_PARAM_SIZE,
1085 ucmd->u.ioctl.param_size);
1086 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_SUBFUNC,
1087 ucmd->u.ioctl.subfunc);
1088 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_CHANNEL,
1089 ucmd->u.ioctl.channel);
1090 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
1091 gccb->gc_scratch_busbase);
1093 /* cache service command */
1094 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
1095 ucmd->u.cache.DeviceNo);
1096 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
1097 ucmd->u.cache.BlockNo);
1098 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
1099 ucmd->u.cache.BlockCnt);
1100 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1102 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1104 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1105 GDT_SG_PTR, gccb->gc_scratch_busbase);
1106 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1107 GDT_SG_LEN, ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE);
1110 /* raw service command */
1111 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
1112 ucmd->u.raw.direction);
1113 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1115 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
1117 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
1119 bcopy(ucmd->u.raw.cmd, gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
1121 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
1123 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
1125 gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
1127 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
1128 ucmd->u.raw.sense_len);
1129 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
1130 gccb->gc_scratch_busbase + ucmd->u.raw.sdlen);
1131 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1133 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1134 GDT_SG_PTR, gccb->gc_scratch_busbase);
1135 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1136 GDT_SG_LEN, ucmd->u.raw.sdlen);
1140 gdt_stat.sg_count_act = 1;
1141 gdt->sc_copy_cmd(gdt, gccb);
1146 gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb)
1150 t = ccb->ccb_h.target_id;
1151 GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cache_cmd(%p, %p, 0x%x, %d)\n",
1152 gdt, ccb, ccb->csio.cdb_io.cdb_bytes[0], t));
1154 switch (ccb->csio.cdb_io.cdb_bytes[0]) {
1155 case TEST_UNIT_READY:
1159 GDT_DPRINTF(GDT_D_MISC, ("REQUEST_SENSE\n"));
1163 struct scsi_inquiry_data inq;
1164 size_t copylen = MIN(sizeof(inq), ccb->csio.dxfer_len);
1166 bzero(&inq, sizeof(inq));
1167 inq.device = (gdt->sc_hdr[t].hd_devtype & 4) ?
1169 inq.dev_qual2 = (gdt->sc_hdr[t].hd_devtype & 1) ? 0x80 : 0;
1170 inq.version = SCSI_REV_2;
1171 inq.response_format = 2;
1172 inq.additional_length = 32;
1173 inq.flags = SID_CmdQue | SID_Sync;
1174 strncpy(inq.vendor, gdt->oem_name, sizeof(inq.vendor));
1175 snprintf(inq.product, sizeof(inq.product),
1176 "Host Drive #%02d", t);
1177 strncpy(inq.revision, " ", sizeof(inq.revision));
1178 bcopy(&inq, ccb->csio.data_ptr, copylen );
1179 if( ccb->csio.dxfer_len > copylen )
1180 bzero( ccb->csio.data_ptr+copylen,
1181 ccb->csio.dxfer_len - copylen );
1187 struct scsi_mode_hdr_6 hd;
1188 struct scsi_mode_block_descr bd;
1189 struct scsi_control_page cp;
1191 size_t copylen = MIN(sizeof(mpd), ccb->csio.dxfer_len);
1194 /*mpd = (struct mpd_data *)ccb->csio.data_ptr;*/
1195 bzero(&mpd, sizeof(mpd));
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);
1204 bcopy(&mpd, ccb->csio.data_ptr, copylen );
1205 if( ccb->csio.dxfer_len > copylen )
1206 bzero( ccb->csio.data_ptr+copylen,
1207 ccb->csio.dxfer_len - copylen );
1208 page=((struct scsi_mode_sense_6 *)ccb->csio.cdb_io.cdb_bytes)->page;
1211 GDT_DPRINTF(GDT_D_MISC, ("MODE_SENSE_6: page 0x%x\n", page));
1218 struct scsi_read_capacity_data rcd;
1219 size_t copylen = MIN(sizeof(rcd), ccb->csio.dxfer_len);
1221 /*rcd = (struct scsi_read_capacity_data *)ccb->csio.data_ptr;*/
1222 bzero(&rcd, sizeof(rcd));
1223 scsi_ulto4b(gdt->sc_hdr[t].hd_size - 1, rcd.addr);
1224 scsi_ulto4b(GDT_SECTOR_SIZE, rcd.length);
1225 bcopy(&rcd, ccb->csio.data_ptr, copylen );
1226 if( ccb->csio.dxfer_len > copylen )
1227 bzero( ccb->csio.data_ptr+copylen,
1228 ccb->csio.dxfer_len - copylen );
1232 GDT_DPRINTF(GDT_D_MISC, ("gdt_internal_cache_cmd(%d) unknown\n",
1233 ccb->csio.cdb_io.cdb_bytes[0]));
1236 ccb->ccb_h.status |= CAM_REQ_CMP;
1237 --gdt_stat.io_count_act;
1242 gdtmapmem(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1244 bus_addr_t *busaddrp;
1246 busaddrp = (bus_addr_t *)arg;
1247 *busaddrp = dm_segs->ds_addr;
1251 gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1253 struct gdt_ccb *gccb;
1255 struct gdt_softc *gdt;
1260 gccb = (struct gdt_ccb *)arg;
1262 gdt = cam_sim_softc((struct cam_sim *)ccb->ccb_h.ccb_sim_ptr);
1264 GDT_DPRINTF(GDT_D_CMD, ("gdtexecuteccb(%p, %p, %p, %d, %d)\n",
1265 gdt, gccb, dm_segs, nseg, error));
1266 gdt_stat.sg_count_act = nseg;
1267 if (nseg > gdt_stat.sg_count_max)
1268 gdt_stat.sg_count_max = nseg;
1270 /* Copy the segments into our SG list */
1271 if (gccb->gc_service == GDT_CACHESERVICE) {
1272 for (i = 0; i < nseg; ++i) {
1273 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1274 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1275 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
1276 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1279 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
1281 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
1284 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
1285 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1287 for (i = 0; i < nseg; ++i) {
1288 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1289 i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
1290 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
1291 i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
1294 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
1296 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
1299 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
1300 nseg * GDT_SG_SZ, sizeof(u_int32_t));
1304 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap,
1305 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
1306 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1309 /* We must NOT abort the command here if CAM_REQ_INPROG is not set,
1310 * because command semaphore is already set!
1313 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1314 /* timeout handling */
1315 ccb->ccb_h.timeout_ch =
1316 timeout(iir_timeout, (caddr_t)gccb,
1317 (ccb->ccb_h.timeout * hz) / 1000);
1319 gdt->sc_copy_cmd(gdt, gccb);
1325 iir_action( struct cam_sim *sim, union ccb *ccb )
1327 struct gdt_softc *gdt;
1328 int lock, bus, target, lun;
1330 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1331 ccb->ccb_h.ccb_sim_ptr = sim;
1332 bus = cam_sim_bus(sim);
1333 target = ccb->ccb_h.target_id;
1334 lun = ccb->ccb_h.target_lun;
1335 GDT_DPRINTF(GDT_D_CMD,
1336 ("iir_action(%p) func 0x%x cmd 0x%x bus %d target %d lun %d\n",
1337 gdt, ccb->ccb_h.func_code, ccb->csio.cdb_io.cdb_bytes[0],
1339 ++gdt_stat.io_count_act;
1340 if (gdt_stat.io_count_act > gdt_stat.io_count_max)
1341 gdt_stat.io_count_max = gdt_stat.io_count_act;
1343 switch (ccb->ccb_h.func_code) {
1346 TAILQ_INSERT_TAIL(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1347 ++gdt_stat.req_queue_act;
1348 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1349 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1353 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1354 case XPT_ABORT: /* Abort the specified CCB */
1356 ccb->ccb_h.status = CAM_REQ_INVALID;
1357 --gdt_stat.io_count_act;
1360 case XPT_SET_TRAN_SETTINGS:
1361 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1362 --gdt_stat.io_count_act;
1365 case XPT_GET_TRAN_SETTINGS:
1366 /* Get default/user set transfer settings for the target */
1368 struct ccb_trans_settings *cts = &ccb->cts;
1369 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
1370 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1372 cts->protocol = PROTO_SCSI;
1373 cts->protocol_version = SCSI_REV_2;
1374 cts->transport = XPORT_SPI;
1375 cts->transport_version = 2;
1377 if (cts->type == CTS_TYPE_USER_SETTINGS) {
1378 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
1379 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1380 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1381 spi->sync_period = 25; /* 10MHz */
1382 if (spi->sync_period != 0)
1383 spi->sync_offset = 15;
1385 spi->valid = CTS_SPI_VALID_SYNC_RATE
1386 | CTS_SPI_VALID_SYNC_OFFSET
1387 | CTS_SPI_VALID_BUS_WIDTH
1388 | CTS_SPI_VALID_DISC;
1389 scsi->valid = CTS_SCSI_VALID_TQ;
1390 ccb->ccb_h.status = CAM_REQ_CMP;
1392 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1394 --gdt_stat.io_count_act;
1398 case XPT_CALC_GEOMETRY:
1400 struct ccb_calc_geometry *ccg;
1401 u_int32_t secs_per_cylinder;
1404 ccg->heads = gdt->sc_hdr[target].hd_heads;
1405 ccg->secs_per_track = gdt->sc_hdr[target].hd_secs;
1406 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1407 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1408 ccb->ccb_h.status = CAM_REQ_CMP;
1409 --gdt_stat.io_count_act;
1413 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1416 ccb->ccb_h.status = CAM_REQ_CMP;
1417 --gdt_stat.io_count_act;
1421 case XPT_TERM_IO: /* Terminate the I/O process */
1423 ccb->ccb_h.status = CAM_REQ_INVALID;
1424 --gdt_stat.io_count_act;
1427 case XPT_PATH_INQ: /* Path routing inquiry */
1429 struct ccb_pathinq *cpi = &ccb->cpi;
1431 cpi->version_num = 1;
1432 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1433 cpi->hba_inquiry |= PI_WIDE_16;
1434 cpi->target_sprt = 1;
1436 cpi->hba_eng_cnt = 0;
1437 if (bus == gdt->sc_virt_bus)
1438 cpi->max_target = GDT_MAX_HDRIVES - 1;
1439 else if (gdt->sc_class & GDT_FC)
1440 cpi->max_target = GDT_MAXID_FC - 1;
1442 cpi->max_target = GDT_MAXID - 1;
1444 cpi->unit_number = cam_sim_unit(sim);
1447 (bus == gdt->sc_virt_bus ? 127 : gdt->sc_bus_id[bus]);
1448 cpi->base_transfer_speed = 3300;
1449 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1450 if (gdt->sc_vendor == INTEL_VENDOR_ID)
1451 strncpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN);
1453 strncpy(cpi->hba_vid, "ICP vortex ", HBA_IDLEN);
1454 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1455 cpi->transport = XPORT_SPI;
1456 cpi->transport_version = 2;
1457 cpi->protocol = PROTO_SCSI;
1458 cpi->protocol_version = SCSI_REV_2;
1459 cpi->ccb_h.status = CAM_REQ_CMP;
1460 --gdt_stat.io_count_act;
1465 GDT_DPRINTF(GDT_D_INVALID, ("gdt_next(%p) cmd 0x%x invalid\n",
1466 gdt, ccb->ccb_h.func_code));
1467 ccb->ccb_h.status = CAM_REQ_INVALID;
1468 --gdt_stat.io_count_act;
1475 iir_poll( struct cam_sim *sim )
1477 struct gdt_softc *gdt;
1479 gdt = (struct gdt_softc *)cam_sim_softc( sim );
1480 GDT_DPRINTF(GDT_D_CMD, ("iir_poll sim %p gdt %p\n", sim, gdt));
1485 iir_timeout(void *arg)
1487 GDT_DPRINTF(GDT_D_TIMEOUT, ("iir_timeout(%p)\n", gccb));
1491 iir_watchdog(void *arg)
1493 struct gdt_softc *gdt;
1495 gdt = (struct gdt_softc *)arg;
1496 GDT_DPRINTF(GDT_D_DEBUG, ("iir_watchdog(%p)\n", gdt));
1499 int ccbs = 0, ucmds = 0, frees = 0, pends = 0;
1504 for (h = TAILQ_FIRST(&gdt->sc_ccb_queue); h != NULL;
1505 h = TAILQ_NEXT(h, sim_links.tqe))
1507 for (u = TAILQ_FIRST(&gdt->sc_ucmd_queue); u != NULL;
1508 u = TAILQ_NEXT(u, links))
1510 for (p = SLIST_FIRST(&gdt->sc_free_gccb); p != NULL;
1511 p = SLIST_NEXT(p, sle))
1513 for (p = SLIST_FIRST(&gdt->sc_pending_gccb); p != NULL;
1514 p = SLIST_NEXT(p, sle))
1517 GDT_DPRINTF(GDT_D_TIMEOUT, ("ccbs %d ucmds %d frees %d pends %d\n",
1518 ccbs, ucmds, frees, pends));
1521 timeout(iir_watchdog, (caddr_t)gdt, hz * 15);
1525 iir_shutdown( void *arg, int howto )
1527 struct gdt_softc *gdt;
1528 struct gdt_ccb *gccb;
1532 gdt = (struct gdt_softc *)arg;
1533 GDT_DPRINTF(GDT_D_CMD, ("iir_shutdown(%p, %d)\n", gdt, howto));
1535 printf("iir%d: Flushing all Host Drives. Please wait ... ",
1538 /* allocate ucmd buffer */
1539 ucmd = malloc(sizeof(gdt_ucmd_t), M_GDTBUF, M_NOWAIT);
1541 printf("iir%d: iir_shutdown(): Cannot allocate resource\n",
1545 bzero(ucmd, sizeof(gdt_ucmd_t));
1547 /* wait for pending IOs */
1549 gdt->sc_state = GDT_SHUTDOWN;
1551 if ((gccb = SLIST_FIRST(&gdt->sc_pending_gccb)) != NULL)
1552 (void) tsleep((void *)gccb, PCATCH | PRIBIO, "iirshw", 100 * hz);
1555 for (i = 0; i < GDT_MAX_HDRIVES; ++i) {
1556 if (gdt->sc_hdr[i].hd_present) {
1557 ucmd->service = GDT_CACHESERVICE;
1558 ucmd->OpCode = GDT_FLUSH;
1559 ucmd->u.cache.DeviceNo = i;
1561 TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
1562 ucmd->complete_flag = FALSE;
1565 if (!ucmd->complete_flag)
1566 (void) tsleep((void *)ucmd, PCATCH|PRIBIO, "iirshw", 10*hz);
1570 free(ucmd, M_DEVBUF);
1577 struct gdt_softc *gdt = arg;
1578 struct gdt_intr_ctx ctx;
1580 struct gdt_ccb *gccb;
1584 GDT_DPRINTF(GDT_D_INTR, ("gdt_intr(%p)\n", gdt));
1586 /* If polling and we were not called from gdt_wait, just return */
1587 if ((gdt->sc_state & GDT_POLLING) &&
1588 !(gdt->sc_state & GDT_POLL_WAIT))
1591 if (!(gdt->sc_state & GDT_POLLING))
1595 ctx.istatus = gdt->sc_get_status(gdt);
1596 if (ctx.istatus == 0x00) {
1597 if (!(gdt->sc_state & GDT_POLLING))
1599 gdt->sc_status = GDT_S_NO_STATUS;
1603 gdt->sc_intr(gdt, &ctx);
1605 gdt->sc_status = ctx.cmd_status;
1606 gdt->sc_service = ctx.service;
1607 gdt->sc_info = ctx.info;
1608 gdt->sc_info2 = ctx.info2;
1610 if (gdt->sc_state & GDT_POLL_WAIT) {
1612 gdt_wait_index = ctx.istatus;
1615 if (ctx.istatus == GDT_ASYNCINDEX) {
1616 gdt_async_event(gdt, ctx.service);
1617 if (!(gdt->sc_state & GDT_POLLING))
1621 if (ctx.istatus == GDT_SPEZINDEX) {
1622 GDT_DPRINTF(GDT_D_INVALID,
1623 ("iir%d: Service unknown or not initialized!\n",
1625 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1626 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1627 gdt_store_event(GDT_ES_DRIVER, 4, &gdt->sc_dvr);
1628 if (!(gdt->sc_state & GDT_POLLING))
1633 gccb = &gdt->sc_gccbs[ctx.istatus - 2];
1634 ctx.service = gccb->gc_service;
1636 switch (gccb->gc_flags) {
1637 case GDT_GCF_UNUSED:
1638 GDT_DPRINTF(GDT_D_INVALID, ("iir%d: Index (%d) to unused command!\n",
1639 gdt->sc_hanum, ctx.istatus));
1640 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
1641 gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
1642 gdt->sc_dvr.eu.driver.index = ctx.istatus;
1643 gdt_store_event(GDT_ES_DRIVER, 1, &gdt->sc_dvr);
1644 gdt_free_ccb(gdt, gccb);
1647 case GDT_GCF_INTERNAL:
1648 if (!(gdt->sc_state & GDT_POLLING))
1653 ucmd = gccb->gc_ucmd;
1654 if (gdt->sc_status == GDT_S_BSY) {
1655 GDT_DPRINTF(GDT_D_DEBUG, ("iir_intr(%p) ioctl: gccb %p busy\n",
1657 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
1658 if (!(gdt->sc_state & GDT_POLLING))
1661 ucmd->status = gdt->sc_status;
1662 ucmd->info = gdt->sc_info;
1663 ucmd->complete_flag = TRUE;
1664 if (ucmd->service == GDT_CACHESERVICE) {
1665 if (ucmd->OpCode == GDT_IOCTL) {
1666 cnt = ucmd->u.ioctl.param_size;
1668 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1670 cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
1672 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1675 cnt = ucmd->u.raw.sdlen;
1677 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1678 if (ucmd->u.raw.sense_len != 0)
1679 bcopy(gccb->gc_scratch, ucmd->data, cnt);
1681 gdt_free_ccb(gdt, gccb);
1682 if (!(gdt->sc_state & GDT_POLLING))
1691 gdt_free_ccb(gdt, gccb);
1692 gdt_sync_event(gdt, ctx.service, ctx.istatus, gccb);
1693 if (!(gdt->sc_state & GDT_POLLING))
1701 gdt_async_event(struct gdt_softc *gdt, int service)
1703 struct gdt_ccb *gccb;
1705 GDT_DPRINTF(GDT_D_INTR, ("gdt_async_event(%p, %d)\n", gdt, service));
1707 if (service == GDT_SCREENSERVICE) {
1708 if (gdt->sc_status == GDT_MSG_REQUEST) {
1709 while (gdt->sc_test_busy(gdt))
1711 gccb = gdt_get_ccb(gdt);
1713 printf("iir%d: No free command index found\n",
1717 bzero(gccb->gc_cmd, GDT_CMD_SZ);
1718 gccb->gc_service = service;
1719 gccb->gc_flags = GDT_GCF_SCREEN;
1720 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
1721 gccb->gc_cmd_index);
1722 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_READ);
1723 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1724 GDT_MSG_INV_HANDLE);
1725 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1726 gccb->gc_scratch_busbase);
1727 gdt->sc_set_sema0(gdt);
1728 gdt->sc_cmd_off = 0;
1729 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1731 gdt->sc_cmd_cnt = 0;
1732 gdt->sc_copy_cmd(gdt, gccb);
1733 printf("iir%d: [PCI %d/%d] ",
1734 gdt->sc_hanum,gdt->sc_bus,gdt->sc_slot);
1735 gdt->sc_release_event(gdt);
1739 if ((gdt->sc_fw_vers & 0xff) >= 0x1a) {
1740 gdt->sc_dvr.size = 0;
1741 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1742 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1743 /* severity and event_string already set! */
1745 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.async);
1746 gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
1747 gdt->sc_dvr.eu.async.service = service;
1748 gdt->sc_dvr.eu.async.status = gdt->sc_status;
1749 gdt->sc_dvr.eu.async.info = gdt->sc_info;
1750 *(u_int32_t *)gdt->sc_dvr.eu.async.scsi_coord = gdt->sc_info2;
1752 gdt_store_event(GDT_ES_ASYNC, service, &gdt->sc_dvr);
1753 printf("iir%d: %s\n", gdt->sc_hanum, gdt->sc_dvr.event_string);
1760 gdt_sync_event(struct gdt_softc *gdt, int service,
1761 u_int8_t index, struct gdt_ccb *gccb)
1765 GDT_DPRINTF(GDT_D_INTR,
1766 ("gdt_sync_event(%p, %d, %d, %p)\n", gdt,service,index,gccb));
1770 if (service == GDT_SCREENSERVICE) {
1773 msg_len = gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_LEN);
1775 if (!(gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1776 gccb->gc_scratch[GDT_SCR_MSG_EXT])) {
1777 gccb->gc_scratch[GDT_SCR_MSG_TEXT + msg_len] = '\0';
1778 printf("%s",&gccb->gc_scratch[GDT_SCR_MSG_TEXT]);
1781 if (gccb->gc_scratch[GDT_SCR_MSG_EXT] &&
1782 !gccb->gc_scratch[GDT_SCR_MSG_ANSWER]) {
1783 while (gdt->sc_test_busy(gdt))
1785 bzero(gccb->gc_cmd, GDT_CMD_SZ);
1786 gccb = gdt_get_ccb(gdt);
1788 printf("iir%d: No free command index found\n",
1792 gccb->gc_service = service;
1793 gccb->gc_flags = GDT_GCF_SCREEN;
1794 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
1795 gccb->gc_cmd_index);
1796 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_READ);
1797 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1798 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1799 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1800 gccb->gc_scratch_busbase);
1801 gdt->sc_set_sema0(gdt);
1802 gdt->sc_cmd_off = 0;
1803 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1805 gdt->sc_cmd_cnt = 0;
1806 gdt->sc_copy_cmd(gdt, gccb);
1807 gdt->sc_release_event(gdt);
1811 if (gccb->gc_scratch[GDT_SCR_MSG_ANSWER] &&
1812 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN)) {
1813 /* default answers (getchar() not possible) */
1814 if (gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) == 1) {
1815 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN, 0);
1816 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 1);
1817 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 0;
1819 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN,
1820 gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) - 2);
1821 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 2);
1822 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 1;
1823 gccb->gc_scratch[GDT_SCR_MSG_TEXT + 1] = 0;
1825 gccb->gc_scratch[GDT_SCR_MSG_EXT] = 0;
1826 gccb->gc_scratch[GDT_SCR_MSG_ANSWER] = 0;
1827 while (gdt->sc_test_busy(gdt))
1829 bzero(gccb->gc_cmd, GDT_CMD_SZ);
1830 gccb = gdt_get_ccb(gdt);
1832 printf("iir%d: No free command index found\n",
1836 gccb->gc_service = service;
1837 gccb->gc_flags = GDT_GCF_SCREEN;
1838 gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
1839 gccb->gc_cmd_index);
1840 gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
1841 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
1842 gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
1843 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
1844 gccb->gc_scratch_busbase);
1845 gdt->sc_set_sema0(gdt);
1846 gdt->sc_cmd_off = 0;
1847 gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ,
1849 gdt->sc_cmd_cnt = 0;
1850 gdt->sc_copy_cmd(gdt, gccb);
1851 gdt->sc_release_event(gdt);
1857 untimeout(iir_timeout, gccb, ccb->ccb_h.timeout_ch);
1858 if (gdt->sc_status == GDT_S_BSY) {
1859 GDT_DPRINTF(GDT_D_DEBUG, ("gdt_sync_event(%p) gccb %p busy\n",
1861 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
1862 ++gdt_stat.req_queue_act;
1863 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
1864 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
1868 bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap,
1869 (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
1870 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1871 bus_dmamap_unload(gdt->sc_buffer_dmat, gccb->gc_dmamap);
1873 ccb->csio.resid = 0;
1874 if (gdt->sc_status == GDT_S_OK) {
1875 ccb->ccb_h.status |= CAM_REQ_CMP;
1876 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1879 if (gccb->gc_service == GDT_CACHESERVICE) {
1880 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1881 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1882 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1883 bzero(&ccb->csio.sense_data, ccb->csio.sense_len);
1884 ccb->csio.sense_data.error_code =
1885 SSD_CURRENT_ERROR | SSD_ERRCODE_VALID;
1886 ccb->csio.sense_data.flags = SSD_KEY_NOT_READY;
1888 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.sync);
1889 gdt->sc_dvr.eu.sync.ionode = gdt->sc_hanum;
1890 gdt->sc_dvr.eu.sync.service = service;
1891 gdt->sc_dvr.eu.sync.status = gdt->sc_status;
1892 gdt->sc_dvr.eu.sync.info = gdt->sc_info;
1893 gdt->sc_dvr.eu.sync.hostdrive = ccb->ccb_h.target_id;
1894 if (gdt->sc_status >= 0x8000)
1895 gdt_store_event(GDT_ES_SYNC, 0, &gdt->sc_dvr);
1897 gdt_store_event(GDT_ES_SYNC, service, &gdt->sc_dvr);
1900 if (gdt->sc_status != GDT_S_RAW_SCSI || gdt->sc_info >= 0x100) {
1901 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1903 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
1904 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1905 ccb->csio.scsi_status = gdt->sc_info;
1906 bcopy(gccb->gc_scratch, &ccb->csio.sense_data,
1907 ccb->csio.sense_len);
1911 --gdt_stat.io_count_act;
1917 /* Controller event handling functions */
1918 gdt_evt_str *gdt_store_event(u_int16_t source, u_int16_t idx,
1924 GDT_DPRINTF(GDT_D_MISC, ("gdt_store_event(%d, %d)\n", source, idx));
1925 if (source == 0) /* no source -> no event */
1928 if (ebuffer[elastidx].event_source == source &&
1929 ebuffer[elastidx].event_idx == idx &&
1930 ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
1931 !memcmp((char *)&ebuffer[elastidx].event_data.eu,
1932 (char *)&evt->eu, evt->size)) ||
1933 (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
1934 !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
1935 (char *)&evt->event_string)))) {
1936 e = &ebuffer[elastidx];
1938 e->last_stamp = tv.tv_sec;
1941 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */
1943 if (elastidx == GDT_MAX_EVENTS)
1945 if (elastidx == eoldidx) { /* reached mark ? */
1947 if (eoldidx == GDT_MAX_EVENTS)
1951 e = &ebuffer[elastidx];
1952 e->event_source = source;
1955 e->first_stamp = e->last_stamp = tv.tv_sec;
1957 e->event_data = *evt;
1963 int gdt_read_event(int handle, gdt_evt_str *estr)
1968 GDT_DPRINTF(GDT_D_MISC, ("gdt_read_event(%d)\n", handle));
1974 estr->event_source = 0;
1976 if (eindex >= GDT_MAX_EVENTS) {
1980 e = &ebuffer[eindex];
1981 if (e->event_source != 0) {
1982 if (eindex != elastidx) {
1983 if (++eindex == GDT_MAX_EVENTS)
1988 memcpy(estr, e, sizeof(gdt_evt_str));
1994 void gdt_readapp_event(u_int8_t application, gdt_evt_str *estr)
2000 GDT_DPRINTF(GDT_D_MISC, ("gdt_readapp_event(%d)\n", application));
2004 e = &ebuffer[eindex];
2005 if (e->event_source == 0)
2007 if ((e->application & application) == 0) {
2008 e->application |= application;
2012 if (eindex == elastidx)
2014 if (++eindex == GDT_MAX_EVENTS)
2018 memcpy(estr, e, sizeof(gdt_evt_str));
2020 estr->event_source = 0;
2024 void gdt_clear_events()
2026 GDT_DPRINTF(GDT_D_MISC, ("gdt_clear_events\n"));
2028 eoldidx = elastidx = 0;
2029 ebuffer[0].event_source = 0;