Style(9) cleanup to src/sys/vfs, stage 20/21: umapfs.
[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.6 2004/05/13 23:49:19 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 "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         /* name */      "iir",
73         /* maj */       CDEV_MAJOR,
74         /* flags */     0,
75         /* port */      NULL,
76         /* clone */     NULL,
77
78         /* open */      iir_open,
79         /* close */     iir_close,
80         /* read */      iir_read,
81         /* write */     iir_write,
82         /* ioctl */     iir_ioctl,
83         /* poll */      nopoll,
84         /* mmap */      nommap,
85         /* strategy */  nostrategy,
86         /* dump */      nodump,
87         /* psize */     nopsize
88 };
89
90 static int iir_devsw_installed = 0;
91 #ifndef SDEV_PER_HBA
92 static int sdev_made = 0;
93 #endif
94 extern int gdt_cnt;
95 extern char ostype[];
96 extern char osrelease[];
97 extern gdt_statist_t gdt_stat;
98
99 /*
100  * Given a controller number,
101  * make a special device and return the dev_t
102  */
103 dev_t 
104 gdt_make_dev(int unit)
105 {
106     dev_t dev;
107
108 #ifdef SDEV_PER_HBA
109     dev = make_dev(&iir_cdevsw, hba2minor(unit), UID_ROOT, GID_OPERATOR,
110                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, "iir%d", unit);
111 #else
112     if (sdev_made)
113         return (0);
114     dev = make_dev(&iir_cdevsw, 0, UID_ROOT, GID_OPERATOR,
115                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, "iir");
116     sdev_made = 1;
117 #endif
118     return (dev);
119 }
120
121 void
122 gdt_destroy_dev(dev_t dev)
123 {
124     if (dev != NULL)
125         destroy_dev(dev);
126 }
127
128 /*
129  * Given a minor device number,
130  * return the pointer to its softc structure
131  */
132 static struct gdt_softc *
133 gdt_minor2softc(int minor_no)
134 {
135     struct gdt_softc *gdt;
136     int hanum;
137
138 #ifdef SDEV_PER_HBA
139     hanum = minor2hba(minor_no);
140 #else
141     hanum = minor_no;
142 #endif
143
144     for (gdt = TAILQ_FIRST(&gdt_softcs);
145          gdt != NULL && gdt->sc_hanum != hanum;
146          gdt = TAILQ_NEXT(gdt, links));
147
148     return (gdt);
149 }
150
151 static int
152 iir_open(dev_t dev, int flags, int fmt, d_thread_t * p)
153 {
154     GDT_DPRINTF(GDT_D_DEBUG, ("iir_open()\n"));
155
156 #ifdef SDEV_PER_HBA
157     int minor_no;
158     struct gdt_softc *gdt;
159
160     minor_no = minor(dev);
161     gdt = gdt_minor2softc(minor_no);
162     if (gdt == NULL)
163         return (ENXIO);
164 #endif
165                 
166     return (0);
167 }
168
169 static int
170 iir_close(dev_t dev, int flags, int fmt, d_thread_t * p)
171 {
172     GDT_DPRINTF(GDT_D_DEBUG, ("iir_close()\n"));
173                 
174 #ifdef SDEV_PER_HBA
175     int minor_no;
176     struct gdt_softc *gdt;
177
178     minor_no = minor(dev);
179     gdt = gdt_minor2softc(minor_no);
180     if (gdt == NULL)
181         return (ENXIO);
182 #endif
183
184     return (0);
185 }
186
187 static int
188 iir_write(dev_t dev, struct uio * uio, int ioflag)
189 {
190     GDT_DPRINTF(GDT_D_DEBUG, ("iir_write()\n"));
191                 
192 #ifdef SDEV_PER_HBA
193     int minor_no;
194     struct gdt_softc *gdt;
195
196     minor_no = minor(dev);
197     gdt = gdt_minor2softc(minor_no);
198     if (gdt == NULL)
199         return (ENXIO);
200 #endif
201
202     return (0);
203 }
204
205 static int
206 iir_read(dev_t dev, struct uio * uio, int ioflag)
207 {
208     GDT_DPRINTF(GDT_D_DEBUG, ("iir_read()\n"));
209                 
210 #ifdef SDEV_PER_HBA
211     int minor_no;
212     struct gdt_softc *gdt;
213
214     minor_no = minor(dev);
215     gdt = gdt_minor2softc(minor_no);
216     if (gdt == NULL)
217         return (ENXIO);
218 #endif
219
220     return (0);
221 }
222
223 /**
224  * This is the control syscall interface.
225  * It should be binary compatible with UnixWare,
226  * if not totally syntatically so.
227  */
228
229 static int
230 iir_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, d_thread_t * p)
231 {
232     GDT_DPRINTF(GDT_D_DEBUG, ("iir_ioctl() cmd 0x%lx\n",cmd));
233
234 #ifdef SDEV_PER_HBA
235     int minor_no;
236     struct gdt_softc *gdt;
237
238     minor_no = minor(dev);
239     gdt = gdt_minor2softc(minor_no);
240     if (gdt == NULL)
241         return (ENXIO);
242 #endif
243     ++gdt_stat.io_count_act;
244     if (gdt_stat.io_count_act > gdt_stat.io_count_max)
245         gdt_stat.io_count_max = gdt_stat.io_count_act;
246
247     switch (cmd) {
248       case GDT_IOCTL_GENERAL:
249         {
250             gdt_ucmd_t *ucmd;
251             struct gdt_softc *gdt;
252             int lock;
253
254             ucmd = (gdt_ucmd_t *)cmdarg;
255             gdt = gdt_minor2softc(ucmd->io_node);
256             if (gdt == NULL)
257                 return (ENXIO);
258             lock = splcam();
259             TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
260             ucmd->complete_flag = FALSE;
261             splx(lock);
262             gdt_next(gdt);
263             if (!ucmd->complete_flag)
264                 (void) tsleep((void *)ucmd, PCATCH, "iirucw", 0);
265             break;
266         }
267
268       case GDT_IOCTL_DRVERS:
269         *(int *)cmdarg = 
270             (IIR_DRIVER_VERSION << 8) | IIR_DRIVER_SUBVERSION;
271         break;
272
273       case GDT_IOCTL_CTRTYPE:
274         {
275             gdt_ctrt_t *p;
276             struct gdt_softc *gdt; 
277             
278             p = (gdt_ctrt_t *)cmdarg;
279             gdt = gdt_minor2softc(p->io_node);
280             if (gdt == NULL)
281                 return (ENXIO);
282             p->oem_id = 0x8000;
283             p->type = 0xfd;
284             p->info = (gdt->sc_bus << 8) | (gdt->sc_slot << 3);
285             p->ext_type = 0x6000 | gdt->sc_subdevice;
286             p->device_id = gdt->sc_device;
287             p->sub_device_id = gdt->sc_subdevice;
288             break;
289         }
290
291       case GDT_IOCTL_OSVERS:
292         {
293             gdt_osv_t *p;
294
295             p = (gdt_osv_t *)cmdarg;
296             p->oscode = 10;
297             p->version = osrelease[0] - '0';
298             if (osrelease[1] == '.')
299                 p->subversion = osrelease[2] - '0';
300             else
301                 p->subversion = 0;
302             if (osrelease[3] == '.')
303                 p->revision = osrelease[4] - '0';
304             else
305                 p->revision = 0;
306             strcpy(p->name, ostype);
307             break;
308         }
309
310       case GDT_IOCTL_CTRCNT:
311         *(int *)cmdarg = gdt_cnt;
312         break;
313
314       case GDT_IOCTL_EVENT:
315         {
316             gdt_event_t *p;
317             int lock;
318
319             p = (gdt_event_t *)cmdarg;
320             if (p->erase == 0xff) {
321                 if (p->dvr.event_source == GDT_ES_TEST)
322                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.test);
323                 else if (p->dvr.event_source == GDT_ES_DRIVER)
324                     p->dvr.event_data.size= sizeof(p->dvr.event_data.eu.driver);
325                 else if (p->dvr.event_source == GDT_ES_SYNC)
326                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.sync);
327                 else
328                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.async);
329                 lock = splcam();
330                 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
331                                 &p->dvr.event_data);
332                 splx(lock);
333             } else if (p->erase == 0xfe) {
334                 lock = splcam();
335                 gdt_clear_events();
336                 splx(lock);
337             } else if (p->erase == 0) {
338                 p->handle = gdt_read_event(p->handle, &p->dvr);
339             } else {
340                 gdt_readapp_event((u_int8_t)p->erase, &p->dvr);
341             }
342             break;
343         }
344         
345       case GDT_IOCTL_STATIST:
346         {
347             gdt_statist_t *p;
348             
349             p = (gdt_statist_t *)cmdarg;
350             bcopy(&gdt_stat, p, sizeof(gdt_statist_t));
351             break;
352         }
353
354       default:
355         break;
356     }
357
358     --gdt_stat.io_count_act;
359     return (0);
360 }
361
362 static void
363 iir_drvinit(void *unused)
364 {
365     GDT_DPRINTF(GDT_D_DEBUG, ("iir_drvinit()\n"));
366                 
367     if (!iir_devsw_installed) {
368         /* Add the I/O (data) channel */
369         cdevsw_add(&iir_cdevsw);
370         iir_devsw_installed = 1;
371     }
372 }
373
374 SYSINIT(iir_dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, iir_drvinit, NULL)