Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / raid / iir / iir_ctrl.c
1 /* $FreeBSD: src/sys/dev/iir/iir_ctrl.c,v 1.2.2.4 2002/05/05 08:18:12 asmodai Exp $ */
2 /* $DragonFly: src/sys/dev/raid/iir/iir_ctrl.c,v 1.2 2003/06/17 04:28:27 dillon Exp $ */
3 /*
4  *       Copyright (c) 2000-01 Intel Corporation
5  *       All Rights Reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * iir_ctrl.c: Control functions and /dev entry points for /dev/iir*
34  *
35  * Written by: Achim Leubner <achim.leubner@intel.com>
36  * Fixes/Additions: Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>
37  *
38  * TODO:     
39  */
40
41 #ident "$Id: iir_ctrl.c 1.2 2001/07/18 11:17:22 achim Exp $"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/uio.h>
48 #include <sys/conf.h>
49 #include <sys/stat.h>
50 #include <sys/disklabel.h>
51 #include <machine/bus.h>
52 #include <vm/vm.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_extern.h>
55 #include <vm/pmap.h>
56
57 #include <dev/iir/iir.h>
58
59 /* Entry points and other prototypes */
60 static struct gdt_softc *gdt_minor2softc(int minor_no);
61
62 static d_open_t         iir_open;
63 static d_close_t        iir_close;
64 static d_write_t        iir_write;
65 static d_read_t         iir_read;
66 static d_ioctl_t        iir_ioctl;
67
68 #define CDEV_MAJOR          IIR_CDEV_MAJOR
69
70 /* Normally, this is a static structure.  But we need it in pci/iir_pci.c */
71 static struct cdevsw iir_cdevsw = {
72         /* open */      iir_open,
73         /* close */     iir_close,
74         /* read */      iir_read,
75         /* write */     iir_write,
76         /* ioctl */     iir_ioctl,
77         /* poll */      nopoll,
78         /* mmap */      nommap,
79         /* strategy */  nostrategy,
80         /* name */      "iir",
81         /* maj */       CDEV_MAJOR,
82         /* dump */      nodump,
83         /* psize */     nopsize,
84         /* flags */     0,
85         /* bmaj */      -1
86 };
87
88 static int iir_devsw_installed = 0;
89 #ifndef SDEV_PER_HBA
90 static int sdev_made = 0;
91 #endif
92 extern int gdt_cnt;
93 extern char ostype[];
94 extern char osrelease[];
95 extern gdt_statist_t gdt_stat;
96
97 /*
98  * Given a controller number,
99  * make a special device and return the dev_t
100  */
101 dev_t 
102 gdt_make_dev(int unit)
103 {
104     dev_t dev;
105
106 #ifdef SDEV_PER_HBA
107     dev = make_dev(&iir_cdevsw, hba2minor(unit), UID_ROOT, GID_OPERATOR,
108                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, "iir%d", unit);
109 #else
110     if (sdev_made)
111         return (0);
112     dev = make_dev(&iir_cdevsw, 0, UID_ROOT, GID_OPERATOR,
113                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, "iir");
114     sdev_made = 1;
115 #endif
116     return (dev);
117 }
118
119 void
120 gdt_destroy_dev(dev_t dev)
121 {
122     if (dev != NULL)
123         destroy_dev(dev);
124 }
125
126 /*
127  * Given a minor device number,
128  * return the pointer to its softc structure
129  */
130 static struct gdt_softc *
131 gdt_minor2softc(int minor_no)
132 {
133     struct gdt_softc *gdt;
134     int hanum;
135
136 #ifdef SDEV_PER_HBA
137     hanum = minor2hba(minor_no);
138 #else
139     hanum = minor_no;
140 #endif
141
142     for (gdt = TAILQ_FIRST(&gdt_softcs);
143          gdt != NULL && gdt->sc_hanum != hanum;
144          gdt = TAILQ_NEXT(gdt, links));
145
146     return (gdt);
147 }
148
149 static int
150 iir_open(dev_t dev, int flags, int fmt, d_thread_t * p)
151 {
152     GDT_DPRINTF(GDT_D_DEBUG, ("iir_open()\n"));
153
154 #ifdef SDEV_PER_HBA
155     int minor_no;
156     struct gdt_softc *gdt;
157
158     minor_no = minor(dev);
159     gdt = gdt_minor2softc(minor_no);
160     if (gdt == NULL)
161         return (ENXIO);
162 #endif
163                 
164     return (0);
165 }
166
167 static int
168 iir_close(dev_t dev, int flags, int fmt, d_thread_t * p)
169 {
170     GDT_DPRINTF(GDT_D_DEBUG, ("iir_close()\n"));
171                 
172 #ifdef SDEV_PER_HBA
173     int minor_no;
174     struct gdt_softc *gdt;
175
176     minor_no = minor(dev);
177     gdt = gdt_minor2softc(minor_no);
178     if (gdt == NULL)
179         return (ENXIO);
180 #endif
181
182     return (0);
183 }
184
185 static int
186 iir_write(dev_t dev, struct uio * uio, int ioflag)
187 {
188     GDT_DPRINTF(GDT_D_DEBUG, ("iir_write()\n"));
189                 
190 #ifdef SDEV_PER_HBA
191     int minor_no;
192     struct gdt_softc *gdt;
193
194     minor_no = minor(dev);
195     gdt = gdt_minor2softc(minor_no);
196     if (gdt == NULL)
197         return (ENXIO);
198 #endif
199
200     return (0);
201 }
202
203 static int
204 iir_read(dev_t dev, struct uio * uio, int ioflag)
205 {
206     GDT_DPRINTF(GDT_D_DEBUG, ("iir_read()\n"));
207                 
208 #ifdef SDEV_PER_HBA
209     int minor_no;
210     struct gdt_softc *gdt;
211
212     minor_no = minor(dev);
213     gdt = gdt_minor2softc(minor_no);
214     if (gdt == NULL)
215         return (ENXIO);
216 #endif
217
218     return (0);
219 }
220
221 /**
222  * This is the control syscall interface.
223  * It should be binary compatible with UnixWare,
224  * if not totally syntatically so.
225  */
226
227 static int
228 iir_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, d_thread_t * p)
229 {
230     GDT_DPRINTF(GDT_D_DEBUG, ("iir_ioctl() cmd 0x%lx\n",cmd));
231
232 #ifdef SDEV_PER_HBA
233     int minor_no;
234     struct gdt_softc *gdt;
235
236     minor_no = minor(dev);
237     gdt = gdt_minor2softc(minor_no);
238     if (gdt == NULL)
239         return (ENXIO);
240 #endif
241     ++gdt_stat.io_count_act;
242     if (gdt_stat.io_count_act > gdt_stat.io_count_max)
243         gdt_stat.io_count_max = gdt_stat.io_count_act;
244
245     switch (cmd) {
246       case GDT_IOCTL_GENERAL:
247         {
248             gdt_ucmd_t *ucmd;
249             struct gdt_softc *gdt;
250             int lock;
251
252             ucmd = (gdt_ucmd_t *)cmdarg;
253             gdt = gdt_minor2softc(ucmd->io_node);
254             if (gdt == NULL)
255                 return (ENXIO);
256             lock = splcam();
257             TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
258             ucmd->complete_flag = FALSE;
259             splx(lock);
260             gdt_next(gdt);
261             if (!ucmd->complete_flag)
262                 (void) tsleep((void *)ucmd, PCATCH | PRIBIO, "iirucw", 0);
263             break;
264         }
265
266       case GDT_IOCTL_DRVERS:
267         *(int *)cmdarg = 
268             (IIR_DRIVER_VERSION << 8) | IIR_DRIVER_SUBVERSION;
269         break;
270
271       case GDT_IOCTL_CTRTYPE:
272         {
273             gdt_ctrt_t *p;
274             struct gdt_softc *gdt; 
275             
276             p = (gdt_ctrt_t *)cmdarg;
277             gdt = gdt_minor2softc(p->io_node);
278             if (gdt == NULL)
279                 return (ENXIO);
280             p->oem_id = 0x8000;
281             p->type = 0xfd;
282             p->info = (gdt->sc_bus << 8) | (gdt->sc_slot << 3);
283             p->ext_type = 0x6000 | gdt->sc_subdevice;
284             p->device_id = gdt->sc_device;
285             p->sub_device_id = gdt->sc_subdevice;
286             break;
287         }
288
289       case GDT_IOCTL_OSVERS:
290         {
291             gdt_osv_t *p;
292
293             p = (gdt_osv_t *)cmdarg;
294             p->oscode = 10;
295             p->version = osrelease[0] - '0';
296             if (osrelease[1] == '.')
297                 p->subversion = osrelease[2] - '0';
298             else
299                 p->subversion = 0;
300             if (osrelease[3] == '.')
301                 p->revision = osrelease[4] - '0';
302             else
303                 p->revision = 0;
304             strcpy(p->name, ostype);
305             break;
306         }
307
308       case GDT_IOCTL_CTRCNT:
309         *(int *)cmdarg = gdt_cnt;
310         break;
311
312       case GDT_IOCTL_EVENT:
313         {
314             gdt_event_t *p;
315             int lock;
316
317             p = (gdt_event_t *)cmdarg;
318             if (p->erase == 0xff) {
319                 if (p->dvr.event_source == GDT_ES_TEST)
320                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.test);
321                 else if (p->dvr.event_source == GDT_ES_DRIVER)
322                     p->dvr.event_data.size= sizeof(p->dvr.event_data.eu.driver);
323                 else if (p->dvr.event_source == GDT_ES_SYNC)
324                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.sync);
325                 else
326                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.async);
327                 lock = splcam();
328                 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
329                                 &p->dvr.event_data);
330                 splx(lock);
331             } else if (p->erase == 0xfe) {
332                 lock = splcam();
333                 gdt_clear_events();
334                 splx(lock);
335             } else if (p->erase == 0) {
336                 p->handle = gdt_read_event(p->handle, &p->dvr);
337             } else {
338                 gdt_readapp_event((u_int8_t)p->erase, &p->dvr);
339             }
340             break;
341         }
342         
343       case GDT_IOCTL_STATIST:
344         {
345             gdt_statist_t *p;
346             
347             p = (gdt_statist_t *)cmdarg;
348             bcopy(&gdt_stat, p, sizeof(gdt_statist_t));
349             break;
350         }
351
352       default:
353         break;
354     }
355
356     --gdt_stat.io_count_act;
357     return (0);
358 }
359
360 static void
361 iir_drvinit(void *unused)
362 {
363     GDT_DPRINTF(GDT_D_DEBUG, ("iir_drvinit()\n"));
364                 
365     if (!iir_devsw_installed) {
366         /* Add the I/O (data) channel */
367         cdevsw_add(&iir_cdevsw);
368         iir_devsw_installed = 1;
369     }
370 }
371
372 SYSINIT(iir_dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, iir_drvinit, NULL)