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