xref: /freebsd/sys/dev/ips/ips_ioctl.c (revision fac71e4e09885bb2afa3d984a0c239a52e1a7418)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Written by: David Jeffery
5  * Copyright (c) 2002 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <dev/ips/ipsreg.h>
34 #include <dev/ips/ips.h>
35 #include <dev/ips/ips_ioctl.h>
36 
37 static void ips_ioctl_finish(ips_command_t *command)
38 {
39 	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);
46 	}
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);
52 }
53 
54 static void ips_ioctl_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
55 {
56 	ips_command_t *command = cmdptr;
57 	ips_ioctl_t *ioctl_cmd = command->arg;
58 	ips_generic_cmd *command_buffer = command->command_buffer;
59 	if(error){
60 		ips_set_error(command, error);
61 		return;
62 	}
63 	command_buffer->id = command->id;
64 	command_buffer->buffaddr = segments[0].ds_addr;
65 	if(ioctl_cmd->readwrite & IPS_IOCTL_WRITE){
66 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
67 				BUS_DMASYNC_PREWRITE);
68 	} else if(ioctl_cmd->readwrite & IPS_IOCTL_READ){
69 		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
70 				BUS_DMASYNC_PREREAD);
71 	}
72 	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
73 			BUS_DMASYNC_PREWRITE);
74 	command->sc->ips_issue_cmd(command);
75 }
76 static int ips_ioctl_start(ips_command_t *command)
77 {
78 	ips_ioctl_t *ioctl_cmd = command->arg;
79 	memcpy(command->command_buffer, ioctl_cmd->command_buffer,
80 		sizeof(ips_generic_cmd));
81 	command->callback = ips_ioctl_finish;
82 	bus_dmamap_load(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
83 			ioctl_cmd->data_buffer,ioctl_cmd->datasize,
84 			ips_ioctl_callback, command, 0);
85 	return 0;
86 }
87 
88 static int ips_ioctl_cmd(ips_softc_t *sc, ips_ioctl_t *ioctl_cmd, ips_user_request *user_request)
89 {
90 	ips_command_t *command;
91 	int error = EINVAL;
92 
93 	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
94 			/* alignment */	1,
95 			/* boundary  */	0,
96 			/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
97 			/* highaddr  */	BUS_SPACE_MAXADDR,
98 			/* filter    */	NULL,
99 			/* filterarg */	NULL,
100 			/* maxsize   */	ioctl_cmd->datasize,
101 			/* numsegs   */	1,
102 			/* maxsegsize*/	ioctl_cmd->datasize,
103 			/* flags     */	0,
104 			/* lockfunc  */ NULL,
105 			/* lockarg   */ NULL,
106 			&ioctl_cmd->dmatag) != 0) {
107 		return ENOMEM;
108         }
109 	if(bus_dmamem_alloc(ioctl_cmd->dmatag, &ioctl_cmd->data_buffer,
110 	   0, &ioctl_cmd->dmamap)){
111 		error = ENOMEM;
112 		goto exit;
113 	}
114 	if(copyin(user_request->data_buffer,ioctl_cmd->data_buffer,
115 	    ioctl_cmd->datasize))
116 		goto exit;
117 	ioctl_cmd->status.value = 0xffffffff;
118 	mtx_lock(&sc->queue_mtx);
119 	if((error = ips_get_free_cmd(sc, &command, 0)) > 0){
120 		error = ENOMEM;
121 		mtx_unlock(&sc->queue_mtx);
122 		goto exit;
123 	}
124 	command->arg = ioctl_cmd;
125 	ips_ioctl_start(command);
126 	while( ioctl_cmd->status.value == 0xffffffff)
127 		msleep(ioctl_cmd, &sc->queue_mtx, 0, "ips", hz/10);
128 	if(COMMAND_ERROR(ioctl_cmd))
129 		error = EIO;
130 	else
131 		error = 0;
132 	mtx_unlock(&sc->queue_mtx);
133 	if(copyout(ioctl_cmd->data_buffer, user_request->data_buffer,
134 	    ioctl_cmd->datasize))
135 		error = EINVAL;
136 	mtx_lock(&sc->queue_mtx);
137 	ips_insert_free_cmd(sc, command);
138 	mtx_unlock(&sc->queue_mtx);
139 
140 exit:	bus_dmamem_free(ioctl_cmd->dmatag, ioctl_cmd->data_buffer,
141 			ioctl_cmd->dmamap);
142 	bus_dma_tag_destroy(ioctl_cmd->dmatag);
143 	return error;
144 }
145 
146 
147 int ips_ioctl_request(ips_softc_t *sc, u_long ioctl_request, caddr_t addr, int32_t flags){
148 	int error = EINVAL;
149 	ips_ioctl_t *ioctl_cmd;
150 	ips_user_request *user_request;
151 	switch(ioctl_request){
152 	case IPS_USER_CMD:
153 		user_request = (ips_user_request *)addr;
154 		ioctl_cmd = malloc(sizeof(ips_ioctl_t), M_IPSBUF, M_WAITOK);
155 		ioctl_cmd->command_buffer = malloc(sizeof(ips_generic_cmd),
156 						M_IPSBUF, M_WAITOK);
157 		if(copyin(user_request->command_buffer,
158 		    ioctl_cmd->command_buffer, sizeof(ips_generic_cmd))){
159 			free(ioctl_cmd->command_buffer, M_IPSBUF);
160 			free(ioctl_cmd, M_IPSBUF);
161 			break;
162 		}
163 		ioctl_cmd->readwrite = IPS_IOCTL_READ | IPS_IOCTL_WRITE;
164 		ioctl_cmd->datasize = IPS_IOCTL_BUFFER_SIZE;
165 		error = ips_ioctl_cmd(sc, ioctl_cmd, user_request);
166 		free(ioctl_cmd->command_buffer, M_IPSBUF);
167 		free(ioctl_cmd, M_IPSBUF);
168 		break;
169 	}
170 
171 	return error;
172 }
173