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