2 * Written by: David Jeffery
3 * Copyright (c) 2002 Adaptec Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/dev/ips/ips_ioctl.c,v 1.5 2004/05/30 04:01:29 scottl Exp $
28 * $DragonFly: src/sys/dev/raid/ips/ips_ioctl.c,v 1.8 2006/09/05 03:48:11 dillon Exp $
31 #include <dev/raid/ips/ips.h>
32 #include <dev/raid/ips/ips_ioctl.h>
33 #include <sys/thread2.h>
36 ips_ioctl_finish(ips_command_t *command)
38 ips_ioctl_t *ioctl_cmd = command->arg;
40 if (ioctl_cmd->readwrite & IPS_IOCTL_READ) {
41 bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
42 BUS_DMASYNC_POSTREAD);
43 } else if (ioctl_cmd->readwrite & IPS_IOCTL_WRITE) {
44 bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
45 BUS_DMASYNC_POSTWRITE);
47 bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
48 BUS_DMASYNC_POSTWRITE);
49 bus_dmamap_unload(ioctl_cmd->dmatag, ioctl_cmd->dmamap);
50 ioctl_cmd->status.value = command->status.value;
51 ips_insert_free_cmd(command->sc, command);
55 ips_ioctl_callback(void *cmdptr, bus_dma_segment_t *segments,
56 int segnum, int error)
58 ips_command_t *command;
59 ips_ioctl_t *ioctl_cmd;
60 ips_generic_cmd *command_buffer;
63 ioctl_cmd = command->arg;
64 command_buffer = command->command_buffer;
66 ioctl_cmd->status.value = IPS_ERROR_STATUS;
67 ips_insert_free_cmd(command->sc, command);
70 command_buffer->id = command->id;
71 command_buffer->buffaddr = segments[0].ds_addr;
72 if (ioctl_cmd->readwrite & IPS_IOCTL_WRITE) {
73 bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
74 BUS_DMASYNC_PREWRITE);
75 } else if (ioctl_cmd->readwrite & IPS_IOCTL_READ) {
76 bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
79 bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
80 BUS_DMASYNC_PREWRITE);
81 command->sc->ips_issue_cmd(command);
85 ips_ioctl_start(ips_command_t *command)
87 ips_ioctl_t *ioctl_cmd = command->arg;
89 memcpy(command->command_buffer, ioctl_cmd->command_buffer,
90 sizeof(ips_generic_cmd));
91 command->callback = ips_ioctl_finish;
92 bus_dmamap_load(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
93 ioctl_cmd->data_buffer, ioctl_cmd->datasize, ips_ioctl_callback,
99 ips_ioctl_cmd(ips_softc_t *sc, ips_ioctl_t *ioctl_cmd,
100 ips_user_request *user_request)
102 ips_command_t *command;
105 if (bus_dma_tag_create(
106 /* parent */ sc->adapter_dmatag,
109 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
110 /* highaddr */ BUS_SPACE_MAXADDR,
112 /* filterarg */ NULL,
113 /* maxsize */ ioctl_cmd->datasize,
115 /* maxsegsize*/ ioctl_cmd->datasize,
117 &ioctl_cmd->dmatag) != 0) {
120 if (bus_dmamem_alloc(ioctl_cmd->dmatag, &ioctl_cmd->data_buffer,
121 0, &ioctl_cmd->dmamap)) {
125 if (copyin(user_request->data_buffer, ioctl_cmd->data_buffer,
126 ioctl_cmd->datasize))
128 ioctl_cmd->status.value = 0xffffffff;
129 lockmgr(&sc->queue_lock, LK_EXCLUSIVE|LK_RETRY);
130 if ((error = ips_get_free_cmd(sc, &command, 0)) > 0) {
132 lockmgr(&sc->queue_lock, LK_RELEASE);
135 command->arg = ioctl_cmd;
136 ips_ioctl_start(command);
137 while (ioctl_cmd->status.value == 0xffffffff)
138 tsleep(ioctl_cmd, 0, "ips", hz / 10);
139 if (COMMAND_ERROR(&ioctl_cmd->status))
143 lockmgr(&sc->queue_lock, LK_RELEASE);
145 if (copyout(ioctl_cmd->data_buffer, user_request->data_buffer,
146 ioctl_cmd->datasize))
149 bus_dmamem_free(ioctl_cmd->dmatag, ioctl_cmd->data_buffer,
151 bus_dma_tag_destroy(ioctl_cmd->dmatag);
156 ips_ioctl_request(ips_softc_t *sc, u_long ioctl_request, caddr_t addr,
159 ips_ioctl_t *ioctl_cmd;
160 ips_user_request *user_request;
163 switch (ioctl_request) {
165 user_request = (ips_user_request *)addr;
166 ioctl_cmd = kmalloc(sizeof(ips_ioctl_t), M_IPSBUF, M_WAITOK);
167 ioctl_cmd->command_buffer = kmalloc(sizeof(ips_generic_cmd),
169 if (copyin(user_request->command_buffer,
170 ioctl_cmd->command_buffer, sizeof(ips_generic_cmd))) {
171 kfree(ioctl_cmd->command_buffer, M_IPSBUF);
172 kfree(ioctl_cmd, M_IPSBUF);
175 ioctl_cmd->readwrite = IPS_IOCTL_READ | IPS_IOCTL_WRITE;
176 ioctl_cmd->datasize = IPS_IOCTL_BUFFER_SIZE;
177 error = ips_ioctl_cmd(sc, ioctl_cmd, user_request);
178 kfree(ioctl_cmd->command_buffer, M_IPSBUF);
179 kfree(ioctl_cmd, M_IPSBUF);