xref: /dragonfly/sys/dev/raid/ips/ips_ioctl.c (revision 606a6e92)
1 /*-
2  * Written by: David Jeffery
3  * Copyright (c) 2002 Adaptec Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
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.4 2004/09/06 16:39:47 joerg Exp $
29  */
30 
31 #include <dev/raid/ips/ips.h>
32 #include <dev/raid/ips/ips_ioctl.h>
33 
34 static void
35 ips_ioctl_finish(ips_command_t *command)
36 {
37 	ips_ioctl_t *ioctl_cmd = command->arg;
38 
39 	if (ioctl_cmd->readwrite & IPS_IOCTL_READ) {
40 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
41 		    BUS_DMASYNC_POSTREAD);
42 	} else if (ioctl_cmd->readwrite & IPS_IOCTL_WRITE) {
43 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
44 		    BUS_DMASYNC_POSTWRITE);
45 	}
46 	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
47 	    BUS_DMASYNC_POSTWRITE);
48 	bus_dmamap_unload(ioctl_cmd->dmatag, ioctl_cmd->dmamap);
49 	ioctl_cmd->status.value = command->status.value;
50 	ips_insert_free_cmd(command->sc, command);
51 }
52 
53 static void
54 ips_ioctl_callback(void *cmdptr, bus_dma_segment_t *segments,
55     int segnum, int error)
56 {
57 	ips_command_t *command;
58 	ips_ioctl_t *ioctl_cmd;
59 	ips_generic_cmd *command_buffer;
60 
61 	command = cmdptr;
62 	ioctl_cmd = command->arg;
63 	command_buffer = command->command_buffer;
64 	if (error) {
65 		ioctl_cmd->status.value = IPS_ERROR_STATUS;
66 		ips_insert_free_cmd(command->sc, command);
67 		return;
68 	}
69 	command_buffer->id = command->id;
70 	command_buffer->buffaddr = segments[0].ds_addr;
71 	if (ioctl_cmd->readwrite & IPS_IOCTL_WRITE) {
72 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
73 		    BUS_DMASYNC_PREWRITE);
74 	} else if (ioctl_cmd->readwrite & IPS_IOCTL_READ) {
75 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
76 		    BUS_DMASYNC_PREREAD);
77 	}
78 	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
79 	    BUS_DMASYNC_PREWRITE);
80 	command->sc->ips_issue_cmd(command);
81 }
82 
83 static int
84 ips_ioctl_start(ips_command_t *command)
85 {
86 	ips_ioctl_t *ioctl_cmd = command->arg;
87 
88 	memcpy(command->command_buffer, ioctl_cmd->command_buffer,
89 	    sizeof(ips_generic_cmd));
90 	command->callback = ips_ioctl_finish;
91 	bus_dmamap_load(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
92 	    ioctl_cmd->data_buffer, ioctl_cmd->datasize, ips_ioctl_callback,
93 	    command, 0);
94 	return 0;
95 }
96 
97 static int
98 ips_ioctl_cmd(ips_softc_t *sc, ips_ioctl_t *ioctl_cmd,
99     ips_user_request *user_request)
100 {
101 	int error = EINVAL;
102 
103 	if (bus_dma_tag_create(
104 			/* parent    */	sc->adapter_dmatag,
105 			/* alignment */	1,
106 			/* boundary  */	0,
107 			/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
108 			/* highaddr  */	BUS_SPACE_MAXADDR,
109 			/* filter    */	NULL,
110 			/* filterarg */	NULL,
111 			/* maxsize   */	ioctl_cmd->datasize,
112 			/* numsegs   */	1,
113 			/* maxsegsize*/	ioctl_cmd->datasize,
114 			/* flags     */	0,
115 			&ioctl_cmd->dmatag) != 0) {
116 		return ENOMEM;
117 	}
118 	if (bus_dmamem_alloc(ioctl_cmd->dmatag, &ioctl_cmd->data_buffer,
119 	    0, &ioctl_cmd->dmamap)) {
120 		error = ENOMEM;
121 		goto exit;
122 	}
123 	if (copyin(user_request->data_buffer, ioctl_cmd->data_buffer,
124 	    ioctl_cmd->datasize))
125 		goto exit;
126 	ioctl_cmd->status.value = 0xffffffff;
127 	if ((error = ips_get_free_cmd(sc, ips_ioctl_start, ioctl_cmd, 0)) > 0) {
128 		error = ENOMEM;
129 		goto exit;
130 	}
131 	while (ioctl_cmd->status.value == 0xffffffff)
132 		tsleep(ioctl_cmd, 0, "ips", hz / 10);
133 	if (COMMAND_ERROR(&ioctl_cmd->status))
134 		error = EIO;
135 	else
136 		error = 0;
137 	if (copyout(ioctl_cmd->data_buffer, user_request->data_buffer,
138 	    ioctl_cmd->datasize))
139 		error = EINVAL;
140 exit:
141 	bus_dmamem_free(ioctl_cmd->dmatag, ioctl_cmd->data_buffer,
142 	    ioctl_cmd->dmamap);
143 	bus_dma_tag_destroy(ioctl_cmd->dmatag);
144 	return error;
145 }
146 
147 int
148 ips_ioctl_request(ips_softc_t *sc, u_long ioctl_request, caddr_t addr,
149     int32_t flags)
150 {
151 	ips_ioctl_t *ioctl_cmd;
152 	ips_user_request *user_request;
153 	int error = EINVAL;
154 
155 	switch (ioctl_request) {
156 	case IPS_USER_CMD:
157 		user_request = (ips_user_request *)addr;
158 		ioctl_cmd = malloc(sizeof(ips_ioctl_t), M_IPSBUF, M_WAITOK);
159 		ioctl_cmd->command_buffer = malloc(sizeof(ips_generic_cmd),
160 		    M_IPSBUF, M_WAITOK);
161 		if (copyin(user_request->command_buffer,
162 		    ioctl_cmd->command_buffer, sizeof(ips_generic_cmd))) {
163 			free(ioctl_cmd->command_buffer, M_IPSBUF);
164 			free(ioctl_cmd, M_IPSBUF);
165 			break;
166 		}
167 		ioctl_cmd->readwrite = IPS_IOCTL_READ | IPS_IOCTL_WRITE;
168 		ioctl_cmd->datasize = IPS_IOCTL_BUFFER_SIZE;
169 		error = ips_ioctl_cmd(sc, ioctl_cmd, user_request);
170 		free(ioctl_cmd->command_buffer, M_IPSBUF);
171 		free(ioctl_cmd, M_IPSBUF);
172 		break;
173 	}
174 	return error;
175 }
176