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