Merge from vendor branch BSDTAR:
[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.7 2004/05/19 22:52:47 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     reference_dev(dev);
119     return (dev);
120 }
121
122 void
123 gdt_destroy_dev(dev_t dev)
124 {
125     if (dev != NULL)
126         destroy_dev(dev);
127 }
128
129 /*
130  * Given a minor device number,
131  * return the pointer to its softc structure
132  */
133 static struct gdt_softc *
134 gdt_minor2softc(int minor_no)
135 {
136     struct gdt_softc *gdt;
137     int hanum;
138
139 #ifdef SDEV_PER_HBA
140     hanum = minor2hba(minor_no);
141 #else
142     hanum = minor_no;
143 #endif
144
145     for (gdt = TAILQ_FIRST(&gdt_softcs);
146          gdt != NULL && gdt->sc_hanum != hanum;
147          gdt = TAILQ_NEXT(gdt, links));
148
149     return (gdt);
150 }
151
152 static int
153 iir_open(dev_t dev, int flags, int fmt, d_thread_t * p)
154 {
155     GDT_DPRINTF(GDT_D_DEBUG, ("iir_open()\n"));
156
157 #ifdef SDEV_PER_HBA
158     int minor_no;
159     struct gdt_softc *gdt;
160
161     minor_no = minor(dev);
162     gdt = gdt_minor2softc(minor_no);
163     if (gdt == NULL)
164         return (ENXIO);
165 #endif
166                 
167     return (0);
168 }
169
170 static int
171 iir_close(dev_t dev, int flags, int fmt, d_thread_t * p)
172 {
173     GDT_DPRINTF(GDT_D_DEBUG, ("iir_close()\n"));
174                 
175 #ifdef SDEV_PER_HBA
176     int minor_no;
177     struct gdt_softc *gdt;
178
179     minor_no = minor(dev);
180     gdt = gdt_minor2softc(minor_no);
181     if (gdt == NULL)
182         return (ENXIO);
183 #endif
184
185     return (0);
186 }
187
188 static int
189 iir_write(dev_t dev, struct uio * uio, int ioflag)
190 {
191     GDT_DPRINTF(GDT_D_DEBUG, ("iir_write()\n"));
192                 
193 #ifdef SDEV_PER_HBA
194     int minor_no;
195     struct gdt_softc *gdt;
196
197     minor_no = minor(dev);
198     gdt = gdt_minor2softc(minor_no);
199     if (gdt == NULL)
200         return (ENXIO);
201 #endif
202
203     return (0);
204 }
205
206 static int
207 iir_read(dev_t dev, struct uio * uio, int ioflag)
208 {
209     GDT_DPRINTF(GDT_D_DEBUG, ("iir_read()\n"));
210                 
211 #ifdef SDEV_PER_HBA
212     int minor_no;
213     struct gdt_softc *gdt;
214
215     minor_no = minor(dev);
216     gdt = gdt_minor2softc(minor_no);
217     if (gdt == NULL)
218         return (ENXIO);
219 #endif
220
221     return (0);
222 }
223
224 /**
225  * This is the control syscall interface.
226  * It should be binary compatible with UnixWare,
227  * if not totally syntatically so.
228  */
229
230 static int
231 iir_ioctl(dev_t dev, u_long cmd, caddr_t cmdarg, int flags, d_thread_t * p)
232 {
233     GDT_DPRINTF(GDT_D_DEBUG, ("iir_ioctl() cmd 0x%lx\n",cmd));
234
235 #ifdef SDEV_PER_HBA
236     int minor_no;
237     struct gdt_softc *gdt;
238
239     minor_no = minor(dev);
240     gdt = gdt_minor2softc(minor_no);
241     if (gdt == NULL)
242         return (ENXIO);
243 #endif
244     ++gdt_stat.io_count_act;
245     if (gdt_stat.io_count_act > gdt_stat.io_count_max)
246         gdt_stat.io_count_max = gdt_stat.io_count_act;
247
248     switch (cmd) {
249       case GDT_IOCTL_GENERAL:
250         {
251             gdt_ucmd_t *ucmd;
252             struct gdt_softc *gdt;
253             int lock;
254
255             ucmd = (gdt_ucmd_t *)cmdarg;
256             gdt = gdt_minor2softc(ucmd->io_node);
257             if (gdt == NULL)
258                 return (ENXIO);
259             lock = splcam();
260             TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
261             ucmd->complete_flag = FALSE;
262             splx(lock);
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             int lock;
319
320             p = (gdt_event_t *)cmdarg;
321             if (p->erase == 0xff) {
322                 if (p->dvr.event_source == GDT_ES_TEST)
323                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.test);
324                 else if (p->dvr.event_source == GDT_ES_DRIVER)
325                     p->dvr.event_data.size= sizeof(p->dvr.event_data.eu.driver);
326                 else if (p->dvr.event_source == GDT_ES_SYNC)
327                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.sync);
328                 else
329                     p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.async);
330                 lock = splcam();
331                 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
332                                 &p->dvr.event_data);
333                 splx(lock);
334             } else if (p->erase == 0xfe) {
335                 lock = splcam();
336                 gdt_clear_events();
337                 splx(lock);
338             } else if (p->erase == 0) {
339                 p->handle = gdt_read_event(p->handle, &p->dvr);
340             } else {
341                 gdt_readapp_event((u_int8_t)p->erase, &p->dvr);
342             }
343             break;
344         }
345         
346       case GDT_IOCTL_STATIST:
347         {
348             gdt_statist_t *p;
349             
350             p = (gdt_statist_t *)cmdarg;
351             bcopy(&gdt_stat, p, sizeof(gdt_statist_t));
352             break;
353         }
354
355       default:
356         break;
357     }
358
359     --gdt_stat.io_count_act;
360     return (0);
361 }
362
363 static void
364 iir_drvinit(void *unused)
365 {
366     GDT_DPRINTF(GDT_D_DEBUG, ("iir_drvinit()\n"));
367                 
368     if (!iir_devsw_installed) {
369         /* Add the I/O (data) channel */
370         cdevsw_add(&iir_cdevsw, 0, 0);
371         iir_devsw_installed = 1;
372     }
373 }
374
375 SYSINIT(iir_dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, iir_drvinit, NULL)