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