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