1114902Sscottl/*-
2119418Sobrien * Written by: David Jeffery
3114902Sscottl * Copyright (c) 2002 Adaptec Inc.
4114902Sscottl * All rights reserved.
5114902Sscottl *
6114902Sscottl * Redistribution and use in source and binary forms, with or without
7114902Sscottl * modification, are permitted provided that the following conditions
8114902Sscottl * are met:
9114902Sscottl * 1. Redistributions of source code must retain the above copyright
10114902Sscottl *    notice, this list of conditions and the following disclaimer.
11114902Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12114902Sscottl *    notice, this list of conditions and the following disclaimer in the
13114902Sscottl *    documentation and/or other materials provided with the distribution.
14114902Sscottl *
15114902Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16114902Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17114902Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18114902Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19114902Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20114902Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21114902Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22114902Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23114902Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24114902Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25114902Sscottl * SUCH DAMAGE.
26114902Sscottl */
27114902Sscottl
28119418Sobrien#include <sys/cdefs.h>
29119418Sobrien__FBSDID("$FreeBSD$");
30114902Sscottl
31152919Sscottl#include <dev/ips/ipsreg.h>
32114902Sscottl#include <dev/ips/ips.h>
33114902Sscottl#include <dev/ips/ips_ioctl.h>
34119418Sobrien
35114902Sscottlstatic void ips_ioctl_finish(ips_command_t *command)
36114902Sscottl{
37114902Sscottl	ips_ioctl_t *ioctl_cmd = command->arg;
38114902Sscottl	if(ioctl_cmd->readwrite & IPS_IOCTL_READ){
39114902Sscottl		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
40114902Sscottl				BUS_DMASYNC_POSTREAD);
41114902Sscottl	} else if(ioctl_cmd->readwrite & IPS_IOCTL_WRITE){
42114902Sscottl		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
43114902Sscottl				BUS_DMASYNC_POSTWRITE);
44114902Sscottl	}
45114902Sscottl	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
46114902Sscottl			BUS_DMASYNC_POSTWRITE);
47114902Sscottl	bus_dmamap_unload(ioctl_cmd->dmatag, ioctl_cmd->dmamap);
48114902Sscottl	ioctl_cmd->status.value = command->status.value;
49114902Sscottl	ips_insert_free_cmd(command->sc, command);
50114902Sscottl}
51114902Sscottl
52114902Sscottlstatic void ips_ioctl_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
53114902Sscottl{
54114902Sscottl	ips_command_t *command = cmdptr;
55114902Sscottl	ips_ioctl_t *ioctl_cmd = command->arg;
56114902Sscottl	ips_generic_cmd *command_buffer = command->command_buffer;
57114902Sscottl	if(error){
58150535Sscottl		ips_set_error(command, error);
59114902Sscottl		return;
60114902Sscottl	}
61114902Sscottl	command_buffer->id = command->id;
62114902Sscottl	command_buffer->buffaddr = segments[0].ds_addr;
63114902Sscottl	if(ioctl_cmd->readwrite & IPS_IOCTL_WRITE){
64114902Sscottl		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
65114902Sscottl				BUS_DMASYNC_PREWRITE);
66114902Sscottl	} else if(ioctl_cmd->readwrite & IPS_IOCTL_READ){
67114902Sscottl		bus_dmamap_sync(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
68114902Sscottl				BUS_DMASYNC_PREREAD);
69114902Sscottl	}
70114902Sscottl	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
71114902Sscottl			BUS_DMASYNC_PREWRITE);
72114902Sscottl	command->sc->ips_issue_cmd(command);
73114902Sscottl}
74114902Sscottlstatic int ips_ioctl_start(ips_command_t *command)
75114902Sscottl{
76114902Sscottl	ips_ioctl_t *ioctl_cmd = command->arg;
77114902Sscottl	memcpy(command->command_buffer, ioctl_cmd->command_buffer,
78114902Sscottl		sizeof(ips_generic_cmd));
79114902Sscottl	command->callback = ips_ioctl_finish;
80114902Sscottl	bus_dmamap_load(ioctl_cmd->dmatag, ioctl_cmd->dmamap,
81114902Sscottl			ioctl_cmd->data_buffer,ioctl_cmd->datasize,
82114902Sscottl			ips_ioctl_callback, command, 0);
83114902Sscottl	return 0;
84114902Sscottl}
85114902Sscottl
86114902Sscottlstatic int ips_ioctl_cmd(ips_softc_t *sc, ips_ioctl_t *ioctl_cmd, ips_user_request *user_request)
87114902Sscottl{
88140923Sscottl	ips_command_t *command;
89114902Sscottl	int error = EINVAL;
90115554Sphk
91115554Sphk	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
92115554Sphk			/* alignment */	1,
93115554Sphk			/* boundary  */	0,
94115554Sphk			/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
95115554Sphk			/* highaddr  */	BUS_SPACE_MAXADDR,
96115554Sphk			/* filter    */	NULL,
97115554Sphk			/* filterarg */	NULL,
98115554Sphk			/* maxsize   */	ioctl_cmd->datasize,
99115554Sphk			/* numsegs   */	1,
100115554Sphk			/* maxsegsize*/	ioctl_cmd->datasize,
101115554Sphk			/* flags     */	0,
102140923Sscottl			/* lockfunc  */ NULL,
103140923Sscottl			/* lockarg   */ NULL,
104115554Sphk			&ioctl_cmd->dmatag) != 0) {
105114902Sscottl		return ENOMEM;
106114902Sscottl        }
107114902Sscottl	if(bus_dmamem_alloc(ioctl_cmd->dmatag, &ioctl_cmd->data_buffer,
108114902Sscottl	   0, &ioctl_cmd->dmamap)){
109114902Sscottl		error = ENOMEM;
110114902Sscottl		goto exit;
111114902Sscottl	}
112114902Sscottl	if(copyin(user_request->data_buffer,ioctl_cmd->data_buffer,
113114902Sscottl	    ioctl_cmd->datasize))
114114902Sscottl		goto exit;
115114902Sscottl	ioctl_cmd->status.value = 0xffffffff;
116140923Sscottl	mtx_lock(&sc->queue_mtx);
117140923Sscottl	if((error = ips_get_free_cmd(sc, &command, 0)) > 0){
118114902Sscottl		error = ENOMEM;
119140923Sscottl		mtx_unlock(&sc->queue_mtx);
120114902Sscottl		goto exit;
121114902Sscottl	}
122140923Sscottl	command->arg = ioctl_cmd;
123140923Sscottl	ips_ioctl_start(command);
124114902Sscottl	while( ioctl_cmd->status.value == 0xffffffff)
125140923Sscottl		msleep(ioctl_cmd, &sc->queue_mtx, 0, "ips", hz/10);
126150535Sscottl	if(COMMAND_ERROR(ioctl_cmd))
127114902Sscottl		error = EIO;
128114902Sscottl	else
129114902Sscottl		error = 0;
130140923Sscottl	mtx_unlock(&sc->queue_mtx);
131114902Sscottl	if(copyout(ioctl_cmd->data_buffer, user_request->data_buffer,
132114902Sscottl	    ioctl_cmd->datasize))
133114902Sscottl		error = EINVAL;
134150535Sscottl	mtx_lock(&sc->queue_mtx);
135150535Sscottl	ips_insert_free_cmd(sc, command);
136150535Sscottl	mtx_unlock(&sc->queue_mtx);
137150535Sscottl
138114902Sscottlexit:	bus_dmamem_free(ioctl_cmd->dmatag, ioctl_cmd->data_buffer,
139114902Sscottl			ioctl_cmd->dmamap);
140114902Sscottl	bus_dma_tag_destroy(ioctl_cmd->dmatag);
141114902Sscottl	return error;
142114902Sscottl}
143114902Sscottl
144114902Sscottl
145114902Sscottlint ips_ioctl_request(ips_softc_t *sc, u_long ioctl_request, caddr_t addr, int32_t flags){
146114902Sscottl	int error = EINVAL;
147114902Sscottl	ips_ioctl_t *ioctl_cmd;
148114902Sscottl	ips_user_request *user_request;
149114902Sscottl	switch(ioctl_request){
150114902Sscottl	case IPS_USER_CMD:
151114902Sscottl		user_request = (ips_user_request *)addr;
152129859Sscottl		ioctl_cmd = malloc(sizeof(ips_ioctl_t), M_IPSBUF, M_WAITOK);
153114902Sscottl		ioctl_cmd->command_buffer = malloc(sizeof(ips_generic_cmd),
154129859Sscottl						M_IPSBUF, M_WAITOK);
155114902Sscottl		if(copyin(user_request->command_buffer,
156114902Sscottl		    ioctl_cmd->command_buffer, sizeof(ips_generic_cmd))){
157129859Sscottl			free(ioctl_cmd->command_buffer, M_IPSBUF);
158129859Sscottl			free(ioctl_cmd, M_IPSBUF);
159114902Sscottl			break;
160114902Sscottl		}
161114902Sscottl		ioctl_cmd->readwrite = IPS_IOCTL_READ | IPS_IOCTL_WRITE;
162114902Sscottl		ioctl_cmd->datasize = IPS_IOCTL_BUFFER_SIZE;
163114902Sscottl		error = ips_ioctl_cmd(sc, ioctl_cmd, user_request);
164129859Sscottl		free(ioctl_cmd->command_buffer, M_IPSBUF);
165129859Sscottl		free(ioctl_cmd, M_IPSBUF);
166114902Sscottl		break;
167114902Sscottl	}
168114902Sscottl
169114902Sscottl	return error;
170114902Sscottl}
171