2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2000-03 ICP vortex GmbH
5 * Copyright (c) 2002-03 Intel Corporation
6 * Copyright (c) 2003 Adaptec Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 * iir_ctrl.c: Control functions and /dev entry points for /dev/iir*
37 * Written by: Achim Leubner <achim_leubner@adaptec.com>
38 * Fixes/Additions: Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>
40 * $Id: iir_ctrl.c 1.3 2003/08/26 12:31:15 achim Exp $"
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/endian.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
56 #include <sys/disklabel.h>
57 #include <sys/sysctl.h>
59 #include <machine/bus.h>
61 #include <dev/iir/iir.h>
63 /* Entry points and other prototypes */
64 static struct gdt_softc *gdt_minor2softc(struct cdev *dev, int minor_no);
66 static d_open_t iir_open;
67 static d_close_t iir_close;
68 static d_write_t iir_write;
69 static d_read_t iir_read;
70 static d_ioctl_t iir_ioctl;
72 /* Normally, this is a static structure. But we need it in pci/iir_pci.c */
73 static struct cdevsw iir_cdevsw = {
74 .d_version = D_VERSION,
84 static int sdev_made = 0;
85 static struct sx sdev_lock;
86 SX_SYSINIT(iir_sdev_lock, &sdev_lock, "iir sdev");
89 extern gdt_statist_t gdt_stat;
92 * Given a controller number,
93 * make a special device and return the dev_t
96 gdt_make_dev(struct gdt_softc *gdt)
101 dev = make_dev(&iir_cdevsw, 0, UID_ROOT, GID_OPERATOR,
102 S_IRUSR | S_IWUSR, "iir%d", unit);
105 sx_xlock(&sdev_lock);
108 dev = make_dev(&iir_cdevsw, 0, UID_ROOT, GID_OPERATOR,
109 S_IRUSR | S_IWUSR, "iir");
111 sx_xunlock(&sdev_lock);
117 gdt_destroy_dev(struct cdev *dev)
124 * Given a minor device number,
125 * return the pointer to its softc structure
127 static struct gdt_softc *
128 gdt_minor2softc(struct cdev *dev, int minor_no)
132 return (dev->si_drv1);
137 dc = devclass_find("iir");
140 child = devclass_get_device(dc, minor_no);
143 return (device_get_softc(child));
148 iir_open(struct cdev *dev, int flags, int fmt, struct thread * p)
150 GDT_DPRINTF(GDT_D_DEBUG, ("iir_open()\n"));
156 iir_close(struct cdev *dev, int flags, int fmt, struct thread * p)
158 GDT_DPRINTF(GDT_D_DEBUG, ("iir_close()\n"));
164 iir_write(struct cdev *dev, struct uio * uio, int ioflag)
166 GDT_DPRINTF(GDT_D_DEBUG, ("iir_write()\n"));
172 iir_read(struct cdev *dev, struct uio * uio, int ioflag)
174 GDT_DPRINTF(GDT_D_DEBUG, ("iir_read()\n"));
180 * This is the control syscall interface.
181 * It should be binary compatible with UnixWare,
182 * if not totally syntatically so.
186 iir_ioctl(struct cdev *dev, u_long cmd, caddr_t cmdarg, int flags, struct thread * p)
188 GDT_DPRINTF(GDT_D_DEBUG, ("iir_ioctl() cmd 0x%lx\n",cmd));
190 ++gdt_stat.io_count_act;
191 if (gdt_stat.io_count_act > gdt_stat.io_count_max)
192 gdt_stat.io_count_max = gdt_stat.io_count_act;
195 case GDT_IOCTL_GENERAL:
198 struct gdt_softc *gdt;
200 ucmd = (gdt_ucmd_t *)cmdarg;
201 gdt = gdt_minor2softc(dev, ucmd->io_node);
204 mtx_lock(&gdt->sc_lock);
205 TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
206 ucmd->complete_flag = FALSE;
208 if (!ucmd->complete_flag)
209 (void) mtx_sleep(ucmd, &gdt->sc_lock, PCATCH | PRIBIO, "iirucw",
211 mtx_unlock(&gdt->sc_lock);
215 case GDT_IOCTL_DRVERS:
216 case GDT_IOCTL_DRVERS_OLD:
218 (IIR_DRIVER_VERSION << 8) | IIR_DRIVER_SUBVERSION;
221 case GDT_IOCTL_CTRTYPE:
222 case GDT_IOCTL_CTRTYPE_OLD:
225 struct gdt_softc *gdt;
227 p = (gdt_ctrt_t *)cmdarg;
228 gdt = gdt_minor2softc(dev, p->io_node);
231 /* only RP controllers */
232 p->ext_type = 0x6000 | gdt->sc_device;
233 if (gdt->sc_vendor == INTEL_VENDOR_ID_IIR) {
234 p->oem_id = OEM_ID_INTEL;
236 /* new -> subdevice into ext_type */
237 if (gdt->sc_device >= 0x600)
238 p->ext_type = 0x6000 | gdt->sc_subdevice;
240 p->oem_id = OEM_ID_ICP;
242 /* new -> subdevice into ext_type */
243 if (gdt->sc_device >= 0x300)
244 p->ext_type = 0x6000 | gdt->sc_subdevice;
246 p->info = (gdt->sc_bus << 8) | (gdt->sc_slot << 3);
247 p->device_id = gdt->sc_device;
248 p->sub_device_id = gdt->sc_subdevice;
252 case GDT_IOCTL_OSVERS:
256 p = (gdt_osv_t *)cmdarg;
258 p->version = osreldate / 100000;
259 p->subversion = osreldate / 1000 % 100;
261 strcpy(p->name, ostype);
265 case GDT_IOCTL_CTRCNT:
266 *(int *)cmdarg = gdt_cnt;
269 case GDT_IOCTL_EVENT:
273 p = (gdt_event_t *)cmdarg;
274 if (p->erase == 0xff) {
275 if (p->dvr.event_source == GDT_ES_TEST)
276 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.test);
277 else if (p->dvr.event_source == GDT_ES_DRIVER)
278 p->dvr.event_data.size= sizeof(p->dvr.event_data.eu.driver);
279 else if (p->dvr.event_source == GDT_ES_SYNC)
280 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.sync);
282 p->dvr.event_data.size = sizeof(p->dvr.event_data.eu.async);
283 gdt_store_event(p->dvr.event_source, p->dvr.event_idx,
285 } else if (p->erase == 0xfe) {
287 } else if (p->erase == 0) {
288 p->handle = gdt_read_event(p->handle, &p->dvr);
290 gdt_readapp_event((u_int8_t)p->erase, &p->dvr);
295 case GDT_IOCTL_STATIST:
299 p = (gdt_statist_t *)cmdarg;
300 bcopy(&gdt_stat, p, sizeof(gdt_statist_t));
308 --gdt_stat.io_count_act;