ips_ioctl.c revision 150535
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
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/ips/ips_ioctl.c 150535 2005-09-25 17:12:41Z scottl $");
30
31#include <dev/ips/ips.h>
32#include <dev/ips/ips_ioctl.h>
33
34static void ips_ioctl_finish(ips_command_t *command)
35{
36	ips_ioctl_t *ioctl_cmd = command->arg;
37	if(ioctl_cmd->readwrite & IPS_IOCTL_READ){
38		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
39				BUS_DMASYNC_POSTREAD);
40	} else if(ioctl_cmd->readwrite & IPS_IOCTL_WRITE){
41		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
42				BUS_DMASYNC_POSTWRITE);
43	}
44	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
45			BUS_DMASYNC_POSTWRITE);
46	bus_dmamap_unload(ioctl_cmd->dmatag, ioctl_cmd->dmamap);
47	ioctl_cmd->status.value = command->status.value;
48	ips_insert_free_cmd(command->sc, command);
49}
50
51static void ips_ioctl_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
52{
53	ips_command_t *command = cmdptr;
54	ips_ioctl_t *ioctl_cmd = command->arg;
55	ips_generic_cmd *command_buffer = command->command_buffer;
56	if(error){
57		ips_set_error(command, error);
58		return;
59	}
60	command_buffer->id = command->id;
61	command_buffer->buffaddr = segments[0].ds_addr;
62	if(ioctl_cmd->readwrite & IPS_IOCTL_WRITE){
63		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
64				BUS_DMASYNC_PREWRITE);
65	} else if(ioctl_cmd->readwrite & IPS_IOCTL_READ){
66		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
67				BUS_DMASYNC_PREREAD);
68	}
69	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
70			BUS_DMASYNC_PREWRITE);
71	command->sc->ips_issue_cmd(command);
72}
73static int ips_ioctl_start(ips_command_t *command)
74{
75	ips_ioctl_t *ioctl_cmd = command->arg;
76	memcpy(command->command_buffer, ioctl_cmd->command_buffer,
77		sizeof(ips_generic_cmd));
78	command->callback = ips_ioctl_finish;
79	bus_dmamap_load(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
80			ioctl_cmd->data_buffer,ioctl_cmd->datasize,
81			ips_ioctl_callback, command, 0);
82	return 0;
83}
84
85static int ips_ioctl_cmd(ips_softc_t *sc, ips_ioctl_t *ioctl_cmd, ips_user_request *user_request)
86{
87	ips_command_t *command;
88	int error = EINVAL;
89
90	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
91			/* alignment */	1,
92			/* boundary  */	0,
93			/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
94			/* highaddr  */	BUS_SPACE_MAXADDR,
95			/* filter    */	NULL,
96			/* filterarg */	NULL,
97			/* maxsize   */	ioctl_cmd->datasize,
98			/* numsegs   */	1,
99			/* maxsegsize*/	ioctl_cmd->datasize,
100			/* flags     */	0,
101			/* lockfunc  */ NULL,
102			/* lockarg   */ NULL,
103			&ioctl_cmd->dmatag) != 0) {
104		return ENOMEM;
105        }
106	if(bus_dmamem_alloc(ioctl_cmd->dmatag, &ioctl_cmd->data_buffer,
107	   0, &ioctl_cmd->dmamap)){
108		error = ENOMEM;
109		goto exit;
110	}
111	if(copyin(user_request->data_buffer,ioctl_cmd->data_buffer,
112	    ioctl_cmd->datasize))
113		goto exit;
114	ioctl_cmd->status.value = 0xffffffff;
115	mtx_lock(&sc->queue_mtx);
116	if((error = ips_get_free_cmd(sc, &command, 0)) > 0){
117		error = ENOMEM;
118		mtx_unlock(&sc->queue_mtx);
119		goto exit;
120	}
121	command->arg = ioctl_cmd;
122	ips_ioctl_start(command);
123	while( ioctl_cmd->status.value == 0xffffffff)
124		msleep(ioctl_cmd, &sc->queue_mtx, 0, "ips", hz/10);
125	if(COMMAND_ERROR(ioctl_cmd))
126		error = EIO;
127	else
128		error = 0;
129	mtx_unlock(&sc->queue_mtx);
130	if(copyout(ioctl_cmd->data_buffer, user_request->data_buffer,
131	    ioctl_cmd->datasize))
132		error = EINVAL;
133	mtx_lock(&sc->queue_mtx);
134	ips_insert_free_cmd(sc, command);
135	mtx_unlock(&sc->queue_mtx);
136
137exit:	bus_dmamem_free(ioctl_cmd->dmatag, ioctl_cmd->data_buffer,
138			ioctl_cmd->dmamap);
139	bus_dma_tag_destroy(ioctl_cmd->dmatag);
140	return error;
141}
142
143
144int ips_ioctl_request(ips_softc_t *sc, u_long ioctl_request, caddr_t addr, int32_t flags){
145	int error = EINVAL;
146	ips_ioctl_t *ioctl_cmd;
147	ips_user_request *user_request;
148	switch(ioctl_request){
149	case IPS_USER_CMD:
150		user_request = (ips_user_request *)addr;
151		ioctl_cmd = malloc(sizeof(ips_ioctl_t), M_IPSBUF, M_WAITOK);
152		ioctl_cmd->command_buffer = malloc(sizeof(ips_generic_cmd),
153						M_IPSBUF, M_WAITOK);
154		if(copyin(user_request->command_buffer,
155		    ioctl_cmd->command_buffer, sizeof(ips_generic_cmd))){
156			free(ioctl_cmd->command_buffer, M_IPSBUF);
157			free(ioctl_cmd, M_IPSBUF);
158			break;
159		}
160		ioctl_cmd->readwrite = IPS_IOCTL_READ | IPS_IOCTL_WRITE;
161		ioctl_cmd->datasize = IPS_IOCTL_BUFFER_SIZE;
162		error = ips_ioctl_cmd(sc, ioctl_cmd, user_request);
163		free(ioctl_cmd->command_buffer, M_IPSBUF);
164		free(ioctl_cmd, M_IPSBUF);
165		break;
166	}
167
168	return error;
169}
170