Merge from vendor branch BINUTILS:
[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.9 2005/06/10 15:46:31 swildner 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 <sys/thread2.h>
52 #include <machine/bus.h>
53 #include <vm/vm.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_extern.h>
56 #include <vm/pmap.h>
57
58 #include "iir.h"
59
60 /* Entry points and other prototypes */
61 static struct gdt_softc *gdt_minor2softc(int minor_no);
62
63 static d_open_t         iir_open;
64 static d_close_t        iir_close;
65 static d_write_t        iir_write;
66 static d_read_t         iir_read;
67 static d_ioctl_t        iir_ioctl;
68
69 #define CDEV_MAJOR          IIR_CDEV_MAJOR
70
71 /* Normally, this is a static structure.  But we need it in pci/iir_pci.c */
72 static struct cdevsw iir_cdevsw = {
73         /* name */      "iir",
74         /* maj */       CDEV_MAJOR,
75         /* flags */     0,
76         /* port */      NULL,
77         /* clone */     NULL,
78
79         /* open */      iir_open,
80         /* close */     iir_close,
81         /* read */      iir_read,
82         /* write */     iir_write,
83         /* ioctl */     iir_ioctl,
84         /* poll */      nopoll,
85         /* mmap */      nommap,
86         /* strategy */  nostrategy,
87         /* dump */      nodump,
88         /* psize */     nopsize
89 };
90
91 static int iir_devsw_installed = 0;
92 #ifndef SDEV_PER_HBA
93 static int sdev_made = 0;
94 #endif
95 extern int gdt_cnt;
96 extern char ostype[];
97 extern char osrelease[];
98 extern gdt_statist_t gdt_stat;
99
100 /*
101  * Given a controller number,
102  * make a special device and return the dev_t
103  */
104 dev_t 
105 gdt_make_dev(int unit)
106 {
107     dev_t dev;
108
109 #ifdef SDEV_PER_HBA
110     dev = make_dev(&iir_cdevsw, hba2minor(unit), UID_ROOT, GID_OPERATOR,
111                    S_IRUSR | S_IWUSR, "iir%d", unit);
112 #else
113     if (sdev_made)
114         return (0);
115     dev = make_dev(&iir_cdevsw, 0, UID_ROOT, GID_OPERATOR,
116                    S_IRUSR | S_IWUSR, "iir");
117     sdev_made = 1;
118 #endif
119     reference_dev(dev);
120     return (dev);
121 }
122
123 void
124 gdt_destroy_dev(dev_t dev)
125 {
126     if (dev != NULL)
127         destroy_dev(dev);
128 }
129
130 /*
131  * Given a minor device number,
132  * return the pointer to its softc structure
133  */
134 static struct gdt_softc *
135 gdt_minor2softc(int minor_no)
136 {
137     struct gdt_softc *gdt;
138     int hanum;
139
140 #ifdef SDEV_PER_HBA
141     hanum = minor2hba(minor_no);
142 #else
143     hanum = minor_no;
144 #endif
145
146     for (gdt = TAILQ_FIRST(&gdt_softcs);
147          gdt != NULL && gdt->sc_hanum != hanum;
148          gdt = TAILQ_NEXT(gdt, links));
149
150     return (gdt);
151 }
152
153 static int
154 iir_open(dev_t dev, int flags, int fmt, d_thread_t * p)
155 {
156     GDT_DPRINTF(GDT_D_DEBUG, ("iir_open()\n"));
157
158 #ifdef SDEV_PER_HBA
159     int minor_no;
160     struct gdt_softc *gdt;
161
162     minor_no = minor(dev);
163     gdt = gdt_minor2softc(minor_no);
164     if (gdt == NULL)
165         return (ENXIO);
166 #endif
167                 
168     return (0);
169 }
170
171 static int
172 iir_close(dev_t dev, int flags, int fmt, d_thread_t * p)
173 {
174     GDT_DPRINTF(GDT_D_DEBUG, ("iir_close()\n"));
175                 
176 #ifdef SDEV_PER_HBA
177     int minor_no;
178     struct gdt_softc *gdt;
179
180     minor_no = minor(dev);
181     gdt = gdt_minor2softc(minor_no);
182     if (gdt == NULL)
183         return (ENXIO);
184 #endif
185
186     return (0);
187 }
188
189 static int
190 iir_write(dev_t dev, struct uio * uio, int ioflag)
191 {
192     GDT_DPRINTF(GDT_D_DEBUG, ("iir_write()\n"));
193                 
194 #ifdef SDEV_PER_HBA
195     int minor_no;
196     struct gdt_softc *gdt;
197
198     minor_no = minor(dev);
199     gdt = gdt_minor2softc(minor_no);
200     if (gdt == NULL)
201         return (ENXIO);
202 #endif
203
204     return (0);
205 }
206
207 static int
208 iir_read(dev_t dev, struct uio * uio, int ioflag)
209 {
210     GDT_DPRINTF(GDT_D_DEBUG, ("iir_read()\n"));
211                 
212 #ifdef SDEV_PER_HBA
213     int minor_no;
214     struct gdt_softc *gdt;
215
216     minor_no = minor(dev);
217     gdt = gdt_minor2softc(minor_no);
218     if (gdt == NULL)
219         return (ENXIO);
220 #endif
221
222     return (0);
223 }
224
225 /**
226  * This is the control syscall interface.
227  * It should be binary compatible with UnixWare,
228  * if not totally syntatically so.
229  */
230
231 static int
232 iir_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, d_thread_t * p)
233 {
234     GDT_DPRINTF(GDT_D_DEBUG, ("iir_ioctl() cmd 0x%lx\n",cmd));
235
236 #ifdef SDEV_PER_HBA
237     int minor_no;
238     struct gdt_softc *gdt;
239
240     minor_no = minor(dev);
241     gdt = gdt_minor2softc(minor_no);
242     if (gdt == NULL)
243         return (ENXIO);
244 #endif
245     ++gdt_stat.io_count_act;
246     if (gdt_stat.io_count_act > gdt_stat.io_count_max)
247         gdt_stat.io_count_max = gdt_stat.io_count_act;
248
249     switch (cmd) {
250       case GDT_IOCTL_GENERAL:
251         {
252             gdt_ucmd_t *ucmd;
253             struct gdt_softc *gdt;
254
255             ucmd = (gdt_ucmd_t *)cmdarg;
256             gdt = gdt_minor2softc(ucmd->io_node);
257             if (gdt == NULL)
258                 return (ENXIO);
259             crit_enter();
260             TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
261             ucmd->complete_flag = FALSE;
262             crit_exit();
263             gdt_next(gdt);
264             if (!ucmd->complete_flag)
265                 (void) tsleep((void *)ucmd, PCATCH, "iirucw", 0);
266             break;
267         }
268
269       case GDT_IOCTL_DRVERS:
270         *(int *)cmdarg = 
271             (IIR_DRIVER_VERSION << 8) | IIR_DRIVER_SUBVERSION;
272         break;
273
274       case GDT_IOCTL_CTRTYPE:
275         {
276             gdt_ctrt_t *p;
277             struct gdt_softc *gdt; 
278             
279             p = (gdt_ctrt_t *)cmdarg;
280             gdt = gdt_minor2softc(p->io_node);
281             if (gdt == NULL)
282                 return (ENXIO);
283             p->oem_id = 0x8000;
284             p->type = 0xfd;
285             p->info = (gdt->sc_bus << 8) | (gdt->sc_slot << 3);
286             p->ext_type = 0x6000 | gdt->sc_subdevice;
287             p->device_id = gdt->sc_device;
288             p->sub_device_id = gdt->sc_subdevice;
289             break;
290         }
291
292       case GDT_IOCTL_OSVERS:
293         {
294             gdt_osv_t *p;
295
296             p = (gdt_osv_t *)cmdarg;
297             p->oscode = 10;
298             p->version = osrelease[0] - '0';
299             if (osrelease[1] == '.')
300                 p->subversion = osrelease[2] - '0';
301             else
302                 p->subversion = 0;
303             if (osrelease[3] == '.')
304                 p->revision = osrelease[4] - '0';
305             else
306                 p->revision = 0;
307             strcpy(p->name, ostype);
308             break;
309         }
310
311       case GDT_IOCTL_CTRCNT:
312         *(int *)cmdarg = gdt_cnt;
313         break;
314
315       case GDT_IOCTL_EVENT:
316         {
317             gdt_event_t *p;
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                 crit_enter();
330                 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
331                                 &p->dvr.event_data);
332                 crit_exit();
333             } else if (p->erase == 0xfe) {
334                 crit_enter();
335                 gdt_clear_events();
336                 crit_exit();
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, 0, 0);
370         iir_devsw_installed = 1;
371     }
372 }
373
374 SYSINIT(iir_dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, iir_drvinit, NULL)