DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / raid / iir / iir_ctrl.c
CommitLineData
7acc60b5 1/* $FreeBSD: src/sys/dev/iir/iir_ctrl.c,v 1.17 2005/05/06 02:32:34 cperciva Exp $ */
aa336941 2/* $DragonFly: src/sys/dev/raid/iir/iir_ctrl.c,v 1.13 2007/05/17 21:08:49 dillon Exp $ */
7acc60b5
SW
3/*-
4 * Copyright (c) 2000-03 ICP vortex GmbH
5 * Copyright (c) 2002-03 Intel Corporation
6 * Copyright (c) 2003 Adaptec Inc.
984263bc
MD
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 *
7acc60b5 37 * Written by: Achim Leubner <achim_leubner@adaptec.com>
984263bc
MD
38 * Fixes/Additions: Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>
39 *
7acc60b5 40 * $Id: iir_ctrl.c 1.3 2003/08/26 12:31:15 achim Exp $"
984263bc
MD
41 */
42
984263bc
MD
43
44#include <sys/param.h>
45#include <sys/systm.h>
7acc60b5 46#include <sys/endian.h>
984263bc
MD
47#include <sys/malloc.h>
48#include <sys/kernel.h>
49#include <sys/uio.h>
1f7ab7c9 50#include <sys/bus.h>
984263bc
MD
51#include <sys/conf.h>
52#include <sys/stat.h>
fef8985e 53#include <sys/device.h>
73c4645c 54#include <sys/thread2.h>
1f7ab7c9 55
984263bc
MD
56#include <vm/vm.h>
57#include <vm/vm_kern.h>
58#include <vm/vm_extern.h>
59#include <vm/pmap.h>
60
1f2de5d4 61#include "iir.h"
984263bc
MD
62
63/* Entry points and other prototypes */
64static struct gdt_softc *gdt_minor2softc(int minor_no);
65
66static d_open_t iir_open;
67static d_close_t iir_close;
68static d_write_t iir_write;
69static d_read_t iir_read;
70static d_ioctl_t iir_ioctl;
71
72#define CDEV_MAJOR IIR_CDEV_MAJOR
73
74/* Normally, this is a static structure. But we need it in pci/iir_pci.c */
fef8985e
MD
75static struct dev_ops iir_ops = {
76 { "iir", CDEV_MAJOR, 0 },
77 .d_open = iir_open,
78 .d_close = iir_close,
79 .d_read = iir_read,
80 .d_write = iir_write,
81 .d_ioctl = iir_ioctl,
984263bc
MD
82};
83
984263bc
MD
84#ifndef SDEV_PER_HBA
85static int sdev_made = 0;
86#endif
87extern int gdt_cnt;
88extern char ostype[];
89extern char osrelease[];
90extern gdt_statist_t gdt_stat;
91
92/*
93 * Given a controller number,
b13267a5 94 * make a special device and return the cdev_t
984263bc 95 */
b13267a5 96cdev_t
984263bc
MD
97gdt_make_dev(int unit)
98{
b13267a5 99 cdev_t dev;
984263bc
MD
100
101#ifdef SDEV_PER_HBA
fef8985e 102 dev = make_dev(&iir_ops, hba2minor(unit), UID_ROOT, GID_OPERATOR,
fb3d7cb1 103 S_IRUSR | S_IWUSR, "iir%d", unit);
984263bc
MD
104#else
105 if (sdev_made)
106 return (0);
fef8985e 107 dev = make_dev(&iir_ops, 0, UID_ROOT, GID_OPERATOR,
fb3d7cb1 108 S_IRUSR | S_IWUSR, "iir");
984263bc
MD
109 sdev_made = 1;
110#endif
e4c9c0c8 111 reference_dev(dev);
984263bc
MD
112 return (dev);
113}
114
115void
b13267a5 116gdt_destroy_dev(cdev_t dev)
984263bc
MD
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 */
126static struct gdt_softc *
127gdt_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
145static int
fef8985e 146iir_open(struct dev_open_args *ap)
984263bc
MD
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
163static int
fef8985e 164iir_close(struct dev_close_args *ap)
984263bc
MD
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
181static int
fef8985e 182iir_write(struct dev_write_args *ap)
984263bc
MD
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
199static int
fef8985e 200iir_read(struct dev_read_args *ap)
984263bc
MD
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
223static int
fef8985e 224iir_ioctl(struct dev_ioctl_args *ap)
984263bc
MD
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
fef8985e 241 switch (ap->a_cmd) {
984263bc
MD
242 case GDT_IOCTL_GENERAL:
243 {
244 gdt_ucmd_t *ucmd;
245 struct gdt_softc *gdt;
984263bc 246
fef8985e 247 ucmd = (gdt_ucmd_t *)ap->a_data;
984263bc
MD
248 gdt = gdt_minor2softc(ucmd->io_node);
249 if (gdt == NULL)
250 return (ENXIO);
73c4645c 251 crit_enter();
984263bc
MD
252 TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
253 ucmd->complete_flag = FALSE;
73c4645c 254 crit_exit();
984263bc
MD
255 gdt_next(gdt);
256 if (!ucmd->complete_flag)
377d4740 257 (void) tsleep((void *)ucmd, PCATCH, "iirucw", 0);
984263bc
MD
258 break;
259 }
260
261 case GDT_IOCTL_DRVERS:
7acc60b5 262 case GDT_IOCTL_DRVERS_OLD:
fef8985e 263 *(int *)ap->a_data =
984263bc
MD
264 (IIR_DRIVER_VERSION << 8) | IIR_DRIVER_SUBVERSION;
265 break;
266
267 case GDT_IOCTL_CTRTYPE:
7acc60b5 268 case GDT_IOCTL_CTRTYPE_OLD:
984263bc
MD
269 {
270 gdt_ctrt_t *p;
271 struct gdt_softc *gdt;
272
fef8985e 273 p = (gdt_ctrt_t *)ap->a_data;
984263bc
MD
274 gdt = gdt_minor2softc(p->io_node);
275 if (gdt == NULL)
276 return (ENXIO);
7acc60b5
SW
277 /* only RP controllers */
278 p->ext_type = 0x6000 | gdt->sc_device;
279 if (gdt->sc_vendor == INTEL_VENDOR_ID) {
280 p->oem_id = OEM_ID_INTEL;
281 p->type = 0xfd;
282 /* new -> subdevice into ext_type */
283 if (gdt->sc_device >= 0x600)
284 p->ext_type = 0x6000 | gdt->sc_subdevice;
285 } else {
286 p->oem_id = OEM_ID_ICP;
287 p->type = 0xfe;
288 /* new -> subdevice into ext_type */
289 if (gdt->sc_device >= 0x300)
290 p->ext_type = 0x6000 | gdt->sc_subdevice;
291 }
984263bc 292 p->info = (gdt->sc_bus << 8) | (gdt->sc_slot << 3);
984263bc
MD
293 p->device_id = gdt->sc_device;
294 p->sub_device_id = gdt->sc_subdevice;
295 break;
296 }
297
298 case GDT_IOCTL_OSVERS:
299 {
300 gdt_osv_t *p;
301
fef8985e 302 p = (gdt_osv_t *)ap->a_data;
984263bc
MD
303 p->oscode = 10;
304 p->version = osrelease[0] - '0';
305 if (osrelease[1] == '.')
306 p->subversion = osrelease[2] - '0';
307 else
308 p->subversion = 0;
309 if (osrelease[3] == '.')
310 p->revision = osrelease[4] - '0';
311 else
312 p->revision = 0;
313 strcpy(p->name, ostype);
314 break;
315 }
316
317 case GDT_IOCTL_CTRCNT:
fef8985e 318 *(int *)ap->a_data = gdt_cnt;
984263bc
MD
319 break;
320
321 case GDT_IOCTL_EVENT:
322 {
323 gdt_event_t *p;
984263bc 324
fef8985e 325 p = (gdt_event_t *)ap->a_data;
984263bc
MD
326 if (p->erase == 0xff) {
327 if (p->dvr.event_source == GDT_ES_TEST)
328 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.test);
329 else if (p->dvr.event_source == GDT_ES_DRIVER)
330 p->dvr.event_data.size= sizeof(p->dvr.event_data.eu.driver);
331 else if (p->dvr.event_source == GDT_ES_SYNC)
332 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.sync);
333 else
334 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.async);
73c4645c 335 crit_enter();
984263bc
MD
336 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
337 &p->dvr.event_data);
73c4645c 338 crit_exit();
984263bc 339 } else if (p->erase == 0xfe) {
73c4645c 340 crit_enter();
984263bc 341 gdt_clear_events();
73c4645c 342 crit_exit();
984263bc
MD
343 } else if (p->erase == 0) {
344 p->handle = gdt_read_event(p->handle, &p->dvr);
345 } else {
346 gdt_readapp_event((u_int8_t)p->erase, &p->dvr);
347 }
348 break;
349 }
350
351 case GDT_IOCTL_STATIST:
352 {
353 gdt_statist_t *p;
354
fef8985e 355 p = (gdt_statist_t *)ap->a_data;
984263bc
MD
356 bcopy(&gdt_stat, p, sizeof(gdt_statist_t));
357 break;
358 }
359
360 default:
361 break;
362 }
363
364 --gdt_stat.io_count_act;
365 return (0);
366}
367