Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / raid / dpt / dpt_control.c
1 /**
2  *       Copyright (c) 1997 by Simon Shapiro
3  *       All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 /**
32  * dpt_control.c: Control Functions and /dev entry points for /dev/dpt*
33  *
34  * Caveat Emptor!       This is work in progress.       The interfaces and
35  * functionality of this code will change (possibly radically) in the
36  * future.
37  */
38
39 #ident "$FreeBSD: src/sys/dev/dpt/dpt_control.c,v 1.16 1999/09/25 18:23:48 phk Exp $"
40
41 #include "opt_dpt.h"
42
43 #include <i386/include/cputypes.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/buf.h>
49 #include <sys/uio.h>
50 #include <sys/conf.h>
51 #include <vm/vm.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_extern.h>
54 #include <vm/pmap.h>
55 #include <scsi/scsiconf.h>
56
57 #include <dev/dpt/dpt.h>
58
59 #define INLINE __inline
60
61 extern char     osrelease[];
62
63 static dpt_sysinfo_t   dpt_sysinfo;
64
65 /* Entry points and other prototypes */
66 static vm_offset_t dpt_physmap(u_int32_t paddr, vm_size_t size);
67 static void     dpt_unphysmap(u_int8_t * vaddr, vm_size_t size);
68
69 static void     dpt_get_sysinfo(void);
70
71 static int      dpt_open(dev_t dev, int flags, int fmt, struct proc * p);
72 static int      dpt_close(dev_t dev, int flags, int fmt, struct proc * p);
73 static int      dpt_write(dev_t dev, struct uio * uio, int ioflag);
74 static int      dpt_read(dev_t dev, struct uio * uio, int ioflag);
75 static int      dpt_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, struct proc * p);
76
77
78 /* This has to be modified as the processor and CPU are not known yet */
79 static dpt_sig_t dpt_sig = {
80         'd', 'P', 't', 'S', 'i', 'G',
81         SIG_VERSION, PROC_INTEL, PROC_386,
82         FT_HBADRVR, FTF_PROTECTED,
83         OEM_DPT, OS_FREEBSD,
84         CAP_PASS | CAP_OVERLAP | CAP_RAID0 | CAP_RAID1 | CAP_RAID5 | CAP_ASPI,
85         DEV_ALL, ADF_SC4_PCI | ADF_SC3_PCI, 0, 0,
86         DPT_RELEASE, DPT_VERSION, DPT_PATCH,
87         DPT_MONTH, DPT_DAY, DPT_YEAR,
88         "DPT FreeBSD Driver (c) 1997 Simon Shapiro"
89 };
90
91 #define CDEV_MAJOR          DPT_CDEV_MAJOR
92
93 /* Normally, this is a static structure.  But we need it in pci/dpt_pci.c */
94 static struct cdevsw dpt_cdevsw = {
95         /* open */      dpt_open,
96         /* close */     dpt_close,
97         /* read */      dpt_read,
98         /* write */     dpt_write,
99         /* ioctl */     dpt_ioctl,
100         /* poll */      nopoll,
101         /* mmap */      nommap,
102         /* strategy */  nostrategy,
103         /* name */      "dpt",
104         /* maj */       CDEV_MAJOR,
105         /* dump */      nodump,
106         /* psize */     nopsize,
107         /* flags */     0,
108         /* bmaj */      -1
109 };
110
111 static struct buf *dpt_inbuf[DPT_MAX_ADAPTERS];
112 static char     dpt_rw_command[DPT_MAX_ADAPTERS][DPT_RW_CMD_LEN + 1];
113
114
115 /*
116  * Given a minor device number,
117  * return the pointer to its softc structure
118  */
119
120 dpt_softc_t *
121 dpt_minor2softc(int minor_no)
122 {
123         dpt_softc_t    *dpt;
124
125         if (dpt_minor2unit(minor_no & ~SCSI_CONTROL_MASK) == -1)
126                 return (NULL);
127
128         for (dpt = TAILQ_FIRST(&dpt_softc_list);
129              (dpt != NULL) && (dpt->unit != (minor_no & ~SCSI_CONTROL_MASK));
130              dpt = TAILQ_NEXT(dpt, links));
131
132         return (dpt);
133 }
134
135 /**
136  * Map a physical address to virtual one.
137  * This is a first cut, experimental thing
138  *
139  * Paddr is the physical address to map
140  * size is the size of the region, in bytes.
141  * Because of alignment problems, we actually round up the size requested to
142  * the next page count.
143  */
144
145 static          vm_offset_t
146 dpt_physmap(u_int32_t req_paddr, vm_size_t req_size)
147 {
148         vm_offset_t     va;
149         int             ndx;
150         vm_size_t       size;
151         u_int32_t       paddr;
152         u_int32_t       offset;
153
154
155
156         size = (req_size / PAGE_SIZE + 1) * PAGE_SIZE;
157         paddr = req_paddr & 0xfffff000;
158         offset = req_paddr - paddr;
159
160         va = kmem_alloc_pageable(kernel_map, size);
161         if (va == (vm_offset_t) 0)
162                 return (va);
163
164         for (ndx = 0; ndx < size; ndx += PAGE_SIZE) {
165                 pmap_kenter(va + ndx, paddr + ndx);
166                 invltlb();
167         }
168
169         return (va + offset);
170 }
171
172
173 /*
174  * Release virtual space allocated by physmap We ASSUME that the correct
175  * start address and the correct LENGTH are given.
176  * 
177  * Disaster will follow if these assumptions are false!
178  */
179
180 static void
181 dpt_unphysmap(u_int8_t * vaddr, vm_size_t size)
182 {
183         int             ndx;
184
185         for (ndx = 0; ndx < size; ndx += PAGE_SIZE) {
186                 pmap_kremove((vm_offset_t) vaddr + ndx);
187         }
188
189         kmem_free(kernel_map, (vm_offset_t) vaddr, size);
190 }
191
192 /**
193  * Collect interesting system information
194  * The following is one of the worst hacks I have ever allowed my
195  * name to be associated with.
196  * There MUST be a system structure that provides this data.
197  */
198
199 static void
200 dpt_get_sysinfo(void)
201 {
202         int             i;
203         int             j;
204         int             ospl;
205         char           *addr;
206
207         bzero(&dpt_sysinfo, sizeof(dpt_sysinfo_t));
208
209         /**
210          * This is really silly, but we better run this in splhigh as we
211          * have no clue what we bump into.
212          * Let's hope anyone else who does this sort of things protects them
213          * with splhigh too.
214          */
215         ospl = splhigh();
216
217         switch (cpu_class) {
218         case CPUCLASS_386:
219                 dpt_sig.Processor = dpt_sysinfo.processorType = PROC_386;
220                 break;
221         case CPUCLASS_486:
222                 dpt_sig.Processor = dpt_sysinfo.processorType = PROC_486;
223                 break;
224         case CPUCLASS_586:
225                 dpt_sig.Processor = dpt_sysinfo.processorType = PROC_PENTIUM;
226                 break;
227         case CPUCLASS_686:
228                 dpt_sig.Processor = dpt_sysinfo.processorType = PROC_P6;
229                 break;
230         default:
231                 dpt_sig.Processor = dpt_sysinfo.flags &= ~SI_ProcessorValid;
232                 break;
233         }
234
235         /* Get The First Drive Type From CMOS */
236         outb(0x70, 0x12);
237         i = inb(0x71);
238         j = i >> 4;
239
240         if (i == 0x0f) {
241                 outb(0x70, 0x19);
242                 j = inb(0x71);
243         }
244         dpt_sysinfo.drive0CMOS = j;
245
246         /* Get The Second Drive Type From CMOS */
247         j = i & 0x0f;
248         if (i == 0x0f) {
249                 outb(0x70, 0x1a);
250                 j = inb(0x71);
251         }
252         dpt_sysinfo.drive1CMOS = j;
253
254         /* Get The Number Of Drives From The Bios Data Area */
255         if ((addr = (char *) dpt_physmap(0x0475, 1024)) == NULL) {
256                 printf("DPT:  Cannot map BIOS address 0x0475.  No sysinfo... :-(\n");
257                 return;
258         }
259         dpt_sysinfo.numDrives = *addr;
260         dpt_unphysmap(addr, 1024);
261
262         /* Get the processor fields from the SIG structure, and set the flags */
263         dpt_sysinfo.processorFamily = dpt_sig.ProcessorFamily;
264         dpt_sysinfo.flags = SI_CMOS_Valid | SI_NumDrivesValid;
265
266         /* Go out and look for SmartROM */
267         for (i = 0; i < 3; ++i) {
268                 switch (i) {
269                 case 0:
270                         addr = (char *) dpt_physmap(0xC8000, 1024);
271                 case 1:
272                         addr = (char *) dpt_physmap(0xD8000, 1024);
273                 default:
274                         addr = (char *) dpt_physmap(0xDC000, 1024);
275                 }
276
277                 if (addr == NULL)
278                         continue;
279
280                 if (*((u_int16_t *) addr) == 0xaa55) {
281                         if ((*((u_int32_t *) (addr + 6)) == 0x00202053)
282                           && (*((u_int32_t *) (addr + 10)) == 0x00545044)) {
283                                 break;
284                         }
285                 }
286                 dpt_unphysmap(addr, 1024);
287                 addr = NULL;
288         }
289
290         /**
291          * If i < 3, we founday it so set up a pointer to the starting
292          * version digit by searching for it.
293          */
294         if (addr != NULL) {
295                 addr += 0x15;
296                 for (i = 0; i < 64; ++i)
297                         if ((addr[i] == ' ') && (addr[i + 1] == 'v'))
298                                 break;
299                 if (i < 64) {
300                         addr += (i + 4);
301                 } else {
302                         dpt_unphysmap(addr, 1024);
303                         addr = NULL;
304                 }
305         }
306         /* If all is well, set up the SmartROM version fields */
307         if (addr != NULL) {
308                 dpt_sysinfo.smartROMMajorVersion = *addr - '0'; /* Assumes ASCII */
309                 dpt_sysinfo.smartROMMinorVersion = *(addr + 2);
310                 dpt_sysinfo.smartROMRevision = *(addr + 3);
311                 dpt_sysinfo.flags |= SI_SmartROMverValid;
312         } else {
313                 dpt_sysinfo.flags |= SI_NO_SmartROM;
314         }
315
316         /* Get the conventional memory size from CMOS */
317         outb(0x70, 0x16);
318         j = inb(0x71);
319         j <<= 8;
320         outb(0x70, 0x15);
321         j |= inb(0x71);
322         dpt_sysinfo.conventionalMemSize = j;
323
324         /**
325          * Get the extended memory found at power on from CMOS
326          */
327         outb(0x70, 0x31);
328         j = inb(0x71);
329         j <<= 8;
330         outb(0x70, 0x30);
331         j |= inb(0x71);
332         dpt_sysinfo.extendedMemSize = j;
333         dpt_sysinfo.flags |= SI_MemorySizeValid;
334
335         /* If there is 1 or 2 drives found, set up the drive parameters */
336         if (dpt_sysinfo.numDrives > 0) {
337                 /* Get the pointer from int 41 for the first drive parameters */
338                 addr = (char *) dpt_physmap(0x0104, 1024);
339
340                 if (addr != NULL) {
341                         j = *((ushort *) (addr + 2));
342                         j *= 16;
343                         j += *((ushort *) (addr));
344                         dpt_unphysmap(addr, 1024);
345                         addr = (char *) dpt_physmap(j, 1024);
346
347                         if (addr != NULL) {
348                                 dpt_sysinfo.drives[0].cylinders = *((ushort *) addr);
349                                 dpt_sysinfo.drives[0].heads = *(addr + 2);
350                                 dpt_sysinfo.drives[0].sectors = *(addr + 14);
351                                 dpt_unphysmap(addr, 1024);
352                         }
353                 }
354                 if (dpt_sysinfo.numDrives > 1) {
355                         /*
356                          * Get the pointer from Int 46 for the second drive
357                          * parameters
358                          */
359                         addr = (char *) dpt_physmap(0x01118, 1024);
360                         j = *((ushort *) (addr + 2));
361                         j *= 16;
362                         j += *((ushort *) (addr));
363                         dpt_unphysmap(addr, 1024);
364                         addr = (char *) dpt_physmap(j, 1024);
365
366                         if (addr != NULL) {
367                                 dpt_sysinfo.drives[1].cylinders = *((ushort *) addr);
368                                 dpt_sysinfo.drives[1].heads = *(addr + 2);
369                                 dpt_sysinfo.drives[1].sectors = *(addr + 14);
370                                 dpt_unphysmap(addr, 1024);
371                         }
372                 }
373                 dpt_sysinfo.flags |= SI_DriveParamsValid;
374         }
375         splx(ospl);
376
377         /* Get the processor information */
378         dpt_sysinfo.flags |= SI_ProcessorValid;
379
380         /* Get the bus I/O bus information */
381         dpt_sysinfo.flags |= SI_BusTypeValid;
382         dpt_sysinfo.busType = HBA_BUS_PCI;
383
384         /* XXX Use _FreeBSD_Version_ */
385         dpt_sysinfo.osType = OS_FREEBSD;
386         dpt_sysinfo.osMajorVersion = osrelease[0] - '0';
387         if (osrelease[1] == '.')
388                 dpt_sysinfo.osMinorVersion = osrelease[2] - '0';
389         else
390                 dpt_sysinfo.osMinorVersion = 0;
391         if (osrelease[3] == '.')
392                 dpt_sysinfo.osRevision = osrelease[4] - '0';
393         else
394                 dpt_sysinfo.osMinorVersion = 0;
395         if (osrelease[5] == '.')
396                 dpt_sysinfo.osSubRevision = osrelease[6] - '0';
397         else
398                 dpt_sysinfo.osMinorVersion = 0;
399
400
401         dpt_sysinfo.flags |= SI_OSversionValid;
402 }
403
404 static int
405 dpt_open(dev_t dev, int flags, int fmt, struct proc * p)
406 {
407         int             minor_no;
408         int             ospl;
409         dpt_softc_t    *dpt;
410
411         minor_no = minor(dev);
412
413         if (dpt_minor2unit(minor_no) == -1)
414                 return (ENXIO);
415         else
416                 dpt = dpt_minor2softc(minor_no);
417
418         if (dpt == NULL)
419                 return (ENXIO);
420
421         ospl = splbio();
422
423         if (dpt->state & DPT_HA_CONTROL_ACTIVE) {
424                 splx(ospl);
425                 return (EBUSY);
426         } else {
427                 if ((dpt_inbuf[minor_no & ~SCSI_CONTROL_MASK] = geteblk(PAGE_SIZE))
428                     == NULL) {
429 #ifdef DPT_DEBUG_CONTROL
430                         printf("dpt%d: Failed to obtain an I/O buffer\n",
431                                minor_no & ~SCSI_CONTROL_MASK);
432 #endif
433                         splx(ospl);
434                         return (EINVAL);
435                 }
436         }
437
438         dpt->state |= DPT_HA_CONTROL_ACTIVE;
439         splx(ospl);
440         return (0);
441 }
442
443 static int
444 dpt_close(dev_t dev, int flags, int fmt, struct proc * p)
445 {
446         int             minor_no;
447         dpt_softc_t    *dpt;
448
449         minor_no = minor(dev);
450         dpt = dpt_minor2softc(minor_no);
451
452         if ((dpt_minor2unit(minor_no) == -1) || (dpt == NULL))
453                 return (ENXIO);
454         else {
455                 brelse(dpt_inbuf[minor_no & ~SCSI_CONTROL_MASK]);
456                 dpt->state &= ~DPT_HA_CONTROL_ACTIVE;
457                 return (0);
458         }
459 }
460
461 static int
462 dpt_write(dev_t dev, struct uio * uio, int ioflag)
463 {
464         int             minor_no;
465         int             unit;
466         int             error;
467
468         minor_no = minor(dev);
469
470         if (minor_no & SCSI_CONTROL_MASK) {
471 #ifdef DPT_DEBUG_CONTROL
472                 printf("dpt%d:  I/O attempted to control channel (%x)\n",
473                        dpt_minor2unit(minor_no), minor_no);
474 #endif
475                 return (ENXIO);
476         }
477         unit = dpt_minor2unit(minor_no);
478
479         if (unit == -1) {
480                 return (ENXIO);
481         } else if (uio->uio_resid > DPT_RW_CMD_LEN) {
482                 return (E2BIG);
483         } else {
484                 char           *cp;
485                 int             length;
486
487                 cp = dpt_inbuf[minor_no]->b_data;
488                 length = uio->uio_resid;        /* uiomove will change it! */
489
490                 if ((error = uiomove(cp, length, uio) != 0)) {
491 #ifdef DPT_DEBUG_CONTROL
492                         printf("dpt%d: uiomove(%x, %d, %x) failed (%d)\n",
493                                minor_no, cp, length, uio, error);
494 #endif
495                         return (error);
496                 } else {
497                         cp[length] = '\0';
498
499                         /* A real kludge, to allow plain echo(1) to work */
500                         if (cp[length - 1] == '\n')
501                                 cp[length - 1] = '\0';
502
503                         strncpy(dpt_rw_command[unit], cp, DPT_RW_CMD_LEN);
504 #ifdef DPT_DEBUG_CONTROL
505                         /**
506                          * For lack of anything better to do;
507                          * For now, dump the data so we can look at it and rejoice
508                          */
509                         printf("dpt%d: Command \"%s\" arrived\n",
510                                unit, dpt_rw_command[unit]);
511 #endif
512                 }
513         }
514
515         return (error);
516 }
517
518 static int
519 dpt_read(dev_t dev, struct uio * uio, int ioflag)
520 {
521         dpt_softc_t    *dpt;
522         int             error;
523         int             minor_no;
524         int             ospl;
525
526         minor_no = minor(dev);
527         error = 0;
528
529 #ifdef DPT_DEBUG_CONTROL
530         printf("dpt%d: read, count = %d, dev = %08x\n",
531                minor_no, uio->uio_resid, dev);
532 #endif
533
534         if (minor_no & SCSI_CONTROL_MASK) {
535 #ifdef DPT_DEBUG_CONTROL
536                 printf("dpt%d:  I/O attempted to control channel (%x)\n",
537                        dpt_minor2unit(minor_no), minor_no);
538 #endif
539                 return (ENXIO);
540         }
541         if (dpt_minor2unit(minor_no) == -1) {
542                 return (ENXIO);
543         }
544         /*
545          * else if ( uio->uio_resid > PAGE_SIZE ) { return(E2BIG); }
546          */ 
547         else {
548                 char           *work_buffer;
549                 char           *wbp;
550                 char           *command;
551                 int             work_size;
552                 int             ndx;
553                 int             x;
554
555                 if ((dpt = dpt_minor2softc(minor_no)) == NULL)
556                         return (ENXIO);
557
558                 work_buffer = (u_int8_t *) malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
559                 wbp = work_buffer;
560                 work_size = 0;
561
562                 ospl = splbio();
563
564                 command = dpt_rw_command[dpt->unit];
565                 if (strcmp(command, DPT_RW_CMD_DUMP_SOFTC) == 0) {
566                         x = sprintf(wbp, "dpt%d:%s:%s:%s:%s:%x\n",
567                                     dpt->unit,
568                                     dpt->board_data.vendor,
569                                     dpt->board_data.modelNum,
570                                     dpt->board_data.firmware,
571                                     dpt->board_data.protocol,
572                                     dpt->EATA_revision);
573                         work_size += x;
574                         wbp += x;
575
576                 } else if (strcmp(command, DPT_RW_CMD_DUMP_SYSINFO) == 0) {
577                         x = sprintf(wbp, "dpt%d:%d:%d:%d:%d:%d:%d:%d:%d:%s:"
578                                     "%d:%d:%d:%d:%d:%d:%d:%d\n",
579                                     dpt->unit,
580                                     dpt_sysinfo.drive0CMOS,
581                                     dpt_sysinfo.drive1CMOS,
582                                     dpt_sysinfo.numDrives,
583                                     dpt_sysinfo.processorFamily,
584                                     dpt_sysinfo.processorType,
585                                     dpt_sysinfo.smartROMMajorVersion,
586                                     dpt_sysinfo.smartROMMinorVersion,
587                                     dpt_sysinfo.smartROMRevision,
588                                     i2bin(dpt_sysinfo.flags,
589                                           sizeof(dpt->queue_status) * 8),
590                                     dpt_sysinfo.conventionalMemSize,
591                                     dpt_sysinfo.extendedMemSize,
592                              dpt_sysinfo.osType, dpt_sysinfo.osMajorVersion,
593                          dpt_sysinfo.osMinorVersion, dpt_sysinfo.osRevision,
594                             dpt_sysinfo.osSubRevision, dpt_sysinfo.busType);
595                         work_size += x;
596                         wbp += x;
597
598                         for (ndx = 0; ndx < 16; ndx++) {
599                                 if (dpt_sysinfo.drives[ndx].cylinders != 0) {
600                                         x = sprintf(wbp, "dpt%d:d%dc%dh%ds%d\n",
601                                                     dpt->unit,
602                                                     ndx,
603                                           dpt_sysinfo.drives[ndx].cylinders,
604                                               dpt_sysinfo.drives[ndx].heads,
605                                            dpt_sysinfo.drives[ndx].sectors);
606                                         work_size += x;
607                                         wbp += x;
608                                 }
609                         }
610                 } else if (strcmp(command, DPT_RW_CMD_DUMP_METRICS) == 0) {
611                         x = sprintf(wbp,
612                                     "dpt%d: No metrics available.\n"
613                                     "Run the dpt_dm command, or use the\n"
614                            "DPT_IOCTL_INTERNAL_METRICS ioctl system call\n",
615                                     dpt->unit);
616                         work_size += x;
617                         wbp += x;
618                 } else if (strcmp(command, DPT_RW_CMD_CLEAR_METRICS) == 0) {
619 #ifdef DPT_MEASURE_PERFORMANCE
620                         dpt_reset_performance(dpt);
621 #endif                          /* DPT_MEASURE_PERFORMANCE */
622
623                         x = sprintf(wbp, "dpt%d: Metrics have been cleared\n",
624                                     dpt->unit);
625                         work_size += x;
626                         wbp += x;
627                 } else if (strcmp(command, DPT_RW_CMD_SHOW_LED) == 0) {
628
629                         x = sprintf(wbp, "dpt%d:%s\n",
630                                 dpt->unit, i2bin(dpt_blinking_led(dpt), 8));
631                         work_size += x;
632                         wbp += x;
633                 } else {
634 #ifdef DPT_DEBUG_CONTROL
635                         printf("dpt%d: Bad READ state (%s)\n", minor_no, command);
636 #endif
637                         splx(ospl);
638                         error = EINVAL;
639                 }
640
641                 if (error == 0) {
642                         work_buffer[work_size++] = '\0';
643                         error = uiomove(work_buffer, work_size, uio);
644                         uio->uio_resid = 0;
645 #ifdef DPT_DEBUG_CONTROL
646                         if (error) {
647                                 printf("dpt%d: READ uimove failed (%d)\n", dpt->unit, error);
648                         }
649 #endif
650                 }
651         }
652         splx(ospl);
653         return (error);
654 }
655
656 /**
657  * This is the control syscall interface.
658  * It should be binary compatible with UnixWare,
659  * if not totally syntatically so.
660  */
661
662 static int
663 dpt_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, struct proc * p)
664 {
665         int             minor_no;
666         dpt_softc_t    *dpt;
667         dpt_user_softc_t udpt;
668         int             result;
669         int             ndx;
670         eata_pt_t      *eata_pass_thru;
671
672         minor_no = minor(dev);
673         result = 0;
674
675         if (!(minor_no & SCSI_CONTROL_MASK)) {
676 #ifdef DPT_DEBUG_CONTROL
677                 printf("dpt%d:  Control attempted to I/O channel (%x)\n",
678                        dpt_minor2unit(minor_no), minor_no);
679 #endif                          /* DEBUG */
680                 return (ENXIO);
681         } else
682                 minor_no &= ~SCSI_CONTROL_MASK;
683
684 #ifdef DPT_DEBUG_CONTROL
685         printf("dpt%d: IOCTL(%x, %x, %p, %x, %p)\n",
686                minor_no, dev, cmd, cmdarg, flags, p);
687 #endif                          /* DEBUG */
688
689         if ((dpt = dpt_minor2softc(minor_no)) == NULL)
690                 return (result);
691
692         switch (cmd) {
693 #ifdef DPT_MEASURE_PERFORMANCE
694         case DPT_IOCTL_INTERNAL_METRICS:            
695                 memcpy(cmdarg, &dpt->performance, sizeof(dpt->performance));
696                 return (0);
697 #endif                          /* DPT_MEASURE_PERFORMANCE */
698         case DPT_IOCTL_SOFTC:
699                 udpt.unit = dpt->unit;
700                 udpt.handle_interrupts = dpt->handle_interrupts;
701                 udpt.target_mode_enabled = dpt->target_mode_enabled;
702                 udpt.spare = dpt->spare;
703
704                 udpt.total_ccbs_count = dpt->total_ccbs_count;
705                 udpt.free_ccbs_count = dpt->free_ccbs_count;
706                 udpt.waiting_ccbs_count = dpt->waiting_ccbs_count;
707                 udpt.submitted_ccbs_count = dpt->submitted_ccbs_count;
708                 udpt.completed_ccbs_count = dpt->completed_ccbs_count;
709
710                 udpt.queue_status = dpt->queue_status;
711                 udpt.free_lock = dpt->free_lock;
712                 udpt.waiting_lock = dpt->waiting_lock;
713                 udpt.submitted_lock = dpt->submitted_lock;
714                 udpt.completed_lock = dpt->completed_lock;
715
716                 udpt.commands_processed = dpt->commands_processed;
717                 udpt.lost_interrupts = dpt->lost_interrupts;
718
719                 udpt.channels = dpt->channels;
720                 udpt.max_id = dpt->max_id;
721                 udpt.max_lun = dpt->max_lun;
722
723                 udpt.io_base = dpt->io_base;
724                 udpt.v_membase = (u_int8_t *) dpt->v_membase;
725                 udpt.p_membase = (u_int8_t *) dpt->p_membase;
726
727                 udpt.irq = dpt->irq;
728                 udpt.dma_channel = dpt->dma_channel;
729
730                 udpt.board_data = dpt->board_data;
731                 udpt.EATA_revision = dpt->EATA_revision;
732                 udpt.bustype = dpt->bustype;
733                 udpt.state = dpt->state;
734
735                 udpt.primary = dpt->primary;
736                 udpt.more_support = dpt->more_support;
737                 udpt.immediate_support = dpt->immediate_support;
738                 udpt.broken_INQUIRY = dpt->broken_INQUIRY;
739                 udpt.spare2 = dpt->spare2;
740
741                 for (ndx = 0; ndx < MAX_CHANNELS; ndx++) {
742                         udpt.resetlevel[ndx] = dpt->resetlevel[ndx];
743                         udpt.hostid[ndx] = dpt->hostid[ndx];
744                 }
745
746                 udpt.last_ccb = dpt->last_ccb;
747                 udpt.cplen = dpt->cplen;
748                 udpt.cppadlen = dpt->cppadlen;
749                 udpt.queuesize = dpt->queuesize;
750                 udpt.sgsize = dpt->sgsize;
751                 udpt.cache_type = dpt->cache_type;
752                 udpt.cache_size = dpt->cache_size;
753
754                 memcpy(cmdarg, &udpt, sizeof(dpt_user_softc_t));
755                 return (0);
756         case SDI_SEND:
757         case DPT_IOCTL_SEND:
758                 eata_pass_thru = (eata_pt_t *) cmdarg;
759
760                 if ((eata_pass_thru->eataID[0] != 'E')
761                     || (eata_pass_thru->eataID[1] != 'A')
762                     || (eata_pass_thru->eataID[2] != 'T')
763                     || (eata_pass_thru->eataID[3] != 'A')) {
764                         return (EFAULT);
765                 }
766                 switch (eata_pass_thru->command) {
767                 case DPT_SIGNATURE:
768                         return (copyout((char *) &dpt_sig,
769                                  (caddr_t *) eata_pass_thru->command_buffer,
770                                         sizeof(dpt_sig)));
771                 case DPT_NUMCTRLS:
772                         return (copyout((char *) &dpt_controllers_present,
773                                  (caddr_t *) eata_pass_thru->command_buffer,
774                                         sizeof(dpt_controllers_present)));
775                 case DPT_CTRLINFO:
776                         {
777                                 dpt_compat_ha_t compat_softc;
778                                 int             ndx;
779
780                                 compat_softc.ha_state = dpt->state;     /* Different Meaning! */
781                                 for (ndx = 0; ndx < MAX_CHANNELS; ndx++)
782                                         compat_softc.ha_id[ndx] = dpt->hostid[ndx];
783
784                                 compat_softc.ha_vect = dpt->irq;
785                                 compat_softc.ha_base = BaseRegister(dpt);
786                                 compat_softc.ha_max_jobs = dpt->total_ccbs_count;
787                                 compat_softc.ha_cache = dpt->cache_type;
788                                 compat_softc.ha_cachesize = dpt->cache_size;
789                                 compat_softc.ha_nbus = dpt->dma_channel + 1;
790                                 compat_softc.ha_ntargets = dpt->max_id + 1;
791                                 compat_softc.ha_nluns = dpt->max_lun + 1;
792                                 compat_softc.ha_tshift = (dpt->max_id == 7) ? 3 : 4;
793                                 compat_softc.ha_bshift = 2;
794                                 compat_softc.ha_npend = dpt->submitted_ccbs_count;
795                                 compat_softc.ha_active_jobs = dpt->waiting_ccbs_count;
796                                 strncpy(compat_softc.ha_fw_version,
797                                     dpt->board_data.firmware,
798                                     sizeof(compat_softc.ha_fw_version));
799                                 compat_softc.ha_ccb = NULL;
800                                 compat_softc.ha_cblist = NULL;
801                                 compat_softc.ha_dev = NULL;
802                                 compat_softc.ha_StPkt_lock = NULL;
803                                 compat_softc.ha_ccb_lock = NULL;
804                                 compat_softc.ha_LuQWaiting = NULL;
805                                 compat_softc.ha_QWait_lock = NULL;
806                                 compat_softc.ha_QWait_opri = NULL;
807
808                                 return (copyout((char *) &compat_softc,
809                                  (caddr_t *) eata_pass_thru->command_buffer,
810                                                 sizeof(dpt_compat_ha_t)));
811                         }
812                         break;
813
814                 case DPT_SYSINFO:
815                         return (copyout((char *) &dpt_sysinfo,
816                                  (caddr_t *) eata_pass_thru->command_buffer,
817                                         sizeof(dpt_sysinfo)));
818                 case EATAUSRCMD:
819                         result = dpt_user_cmd(dpt, eata_pass_thru, cmdarg, minor_no);
820                         return (result);
821                 case DPT_BLINKLED:
822                         result = dpt_blinking_led(dpt);
823                         return (copyout((caddr_t) & result,
824                                  (caddr_t *) eata_pass_thru->command_buffer,
825                                         sizeof(result)));
826                 default:
827                         printf("dpt%d: Invalid (%x) pass-throu command\n",
828                                dpt->unit, eata_pass_thru->command);
829                         result = EINVAL;
830                 }
831
832         default:
833                 printf("dpt%d: Invalid (%lx) IOCTL\n", dpt->unit, cmd);
834                 return (EINVAL);
835
836         }
837
838         return (result);
839 }
840
841 static          dpt_devsw_installed = 0;
842
843 static void
844 dpt_drvinit(void *unused)
845 {
846
847         if (!dpt_devsw_installed) {
848                 if (bootverbose)
849                         printf("DPT:  RAID Manager driver, Version %d.%d.%d\n",
850                                DPT_CTL_RELEASE, DPT_CTL_VERSION, DPT_CTL_PATCH);
851
852                 /* Add the I/O (data) channel */
853                 cdevsw_add(&dpt_cdevsw);
854
855                 dpt_devsw_installed = 1;
856         }
857         dpt_get_sysinfo();
858 }
859
860 SYSINIT(dpt_dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, dpt_drvinit, NULL)
861 /* End of the dpt_control driver */