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