xref: /titanic_50/usr/src/uts/common/io/aac/aac_ioctl.c (revision 79659ce54ea319c9c890f6dd5dbeb1146d725cb9)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright 2005-06 Adaptec, Inc.
8  * Copyright (c) 2005-06 Adaptec Inc., Achim Leubner
9  * Copyright (c) 2000 Michael Smith
10  * Copyright (c) 2001 Scott Long
11  * Copyright (c) 2000 BSDi
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/modctl.h>
38 #include <sys/conf.h>
39 #include <sys/cmn_err.h>
40 #include <sys/ddi.h>
41 #include <sys/devops.h>
42 #include <sys/pci.h>
43 #include <sys/types.h>
44 #include <sys/ddidmareq.h>
45 #include <sys/scsi/scsi.h>
46 #include <sys/ksynch.h>
47 #include <sys/sunddi.h>
48 #include <sys/byteorder.h>
49 #include <sys/kmem.h>
50 #include "aac_regs.h"
51 #include "aac.h"
52 #include "aac_ioctl.h"
53 
54 struct aac_umem_sge {
55 	uint32_t bcount;
56 	caddr_t addr;
57 	struct aac_cmd acp;
58 };
59 
60 /*
61  * External functions
62  */
63 extern int aac_sync_mbcommand(struct aac_softstate *, uint32_t, uint32_t,
64     uint32_t, uint32_t, uint32_t, uint32_t *);
65 extern int aac_cmd_dma_alloc(struct aac_softstate *, struct aac_cmd *,
66     struct buf *, int, int (*)(), caddr_t);
67 extern void aac_free_dmamap(struct aac_cmd *);
68 extern int aac_do_io(struct aac_softstate *, struct aac_cmd *);
69 extern void aac_cmd_fib_copy(struct aac_softstate *, struct aac_cmd *);
70 extern void aac_ioctl_complete(struct aac_softstate *, struct aac_cmd *);
71 
72 extern ddi_device_acc_attr_t aac_acc_attr;
73 extern int aac_check_dma_handle(ddi_dma_handle_t);
74 
75 /*
76  * IOCTL command handling functions
77  */
78 static int aac_check_revision(struct aac_softstate *, intptr_t, int);
79 static int aac_ioctl_send_fib(struct aac_softstate *, intptr_t, int);
80 static int aac_open_getadapter_fib(struct aac_softstate *, intptr_t, int);
81 static int aac_next_getadapter_fib(struct aac_softstate *, intptr_t, int);
82 static int aac_close_getadapter_fib(struct aac_softstate *, intptr_t);
83 static int aac_send_raw_srb(struct aac_softstate *, dev_t, intptr_t, int);
84 static int aac_get_pci_info(struct aac_softstate *, intptr_t, int);
85 static int aac_query_disk(struct aac_softstate *, intptr_t, int);
86 static int aac_delete_disk(struct aac_softstate *, intptr_t, int);
87 static int aac_supported_features(struct aac_softstate *, intptr_t, int);
88 
89 /*
90  * Warlock directives
91  */
92 _NOTE(SCHEME_PROTECTS_DATA("unique to each handling function", aac_features
93     aac_pci_info aac_query_disk aac_revision aac_umem_sge))
94 
95 int
96 aac_do_ioctl(struct aac_softstate *softs, dev_t dev, int cmd, intptr_t arg,
97     int mode)
98 {
99 	int status;
100 
101 	switch (cmd) {
102 	case FSACTL_MINIPORT_REV_CHECK:
103 		AACDB_PRINT_IOCTL(softs, "FSACTL_MINIPORT_REV_CHECK");
104 		status = aac_check_revision(softs, arg, mode);
105 		break;
106 	case FSACTL_SENDFIB:
107 		AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_LARGE_FIB");
108 		goto send_fib;
109 	case FSACTL_SEND_LARGE_FIB:
110 		AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_LARGE_FIB");
111 send_fib:
112 		status = aac_ioctl_send_fib(softs, arg, mode);
113 		break;
114 	case FSACTL_OPEN_GET_ADAPTER_FIB:
115 		AACDB_PRINT_IOCTL(softs, "FSACTL_OPEN_GET_ADAPTER_FIB");
116 		status = aac_open_getadapter_fib(softs, arg, mode);
117 		break;
118 	case FSACTL_GET_NEXT_ADAPTER_FIB:
119 		AACDB_PRINT_IOCTL(softs, "FSACTL_GET_NEXT_ADAPTER_FIB");
120 		status = aac_next_getadapter_fib(softs, arg, mode);
121 		break;
122 	case FSACTL_CLOSE_GET_ADAPTER_FIB:
123 		AACDB_PRINT_IOCTL(softs, "FSACTL_CLOSE_GET_ADAPTER_FIB");
124 		status = aac_close_getadapter_fib(softs, arg);
125 		break;
126 	case FSACTL_SEND_RAW_SRB:
127 		AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_RAW_SRB");
128 		status = aac_send_raw_srb(softs, dev, arg, mode);
129 		break;
130 	case FSACTL_GET_PCI_INFO:
131 		AACDB_PRINT_IOCTL(softs, "FSACTL_GET_PCI_INFO");
132 		status = aac_get_pci_info(softs, arg, mode);
133 		break;
134 	case FSACTL_QUERY_DISK:
135 		AACDB_PRINT_IOCTL(softs, "FSACTL_QUERY_DISK");
136 		status = aac_query_disk(softs, arg, mode);
137 		break;
138 	case FSACTL_DELETE_DISK:
139 		AACDB_PRINT_IOCTL(softs, "FSACTL_DELETE_DISK");
140 		status = aac_delete_disk(softs, arg, mode);
141 		break;
142 	case FSACTL_GET_FEATURES:
143 		AACDB_PRINT_IOCTL(softs, "FSACTL_GET_FEATURES");
144 		status = aac_supported_features(softs, arg, mode);
145 		break;
146 	default:
147 		status = ENOTTY;
148 		AACDB_PRINT(softs, CE_WARN,
149 		    "!IOCTL cmd 0x%x not supported", cmd);
150 		break;
151 	}
152 
153 	return (status);
154 }
155 
156 /*ARGSUSED*/
157 static int
158 aac_check_revision(struct aac_softstate *softs, intptr_t arg, int mode)
159 {
160 	union aac_revision_align un;
161 	struct aac_revision *aac_rev = &un.d;
162 
163 	DBCALLED(softs, 2);
164 
165 	/* Copyin the revision struct from userspace */
166 	if (ddi_copyin((void *)arg, aac_rev,
167 	    sizeof (struct aac_revision), mode) != 0)
168 		return (EFAULT);
169 
170 	/* Doctor up the response struct */
171 	aac_rev->compat = 1;
172 	aac_rev->version =
173 	    ((uint32_t)AAC_DRIVER_MAJOR_VERSION << 24) |
174 	    ((uint32_t)AAC_DRIVER_MINOR_VERSION << 16) |
175 	    ((uint32_t)AAC_DRIVER_TYPE << 8) |
176 	    ((uint32_t)AAC_DRIVER_BUGFIX_LEVEL);
177 	aac_rev->build = (uint32_t)AAC_DRIVER_BUILD;
178 
179 	if (ddi_copyout(aac_rev, (void *)arg,
180 	    sizeof (struct aac_revision), mode) != 0)
181 		return (EFAULT);
182 
183 	return (0);
184 }
185 
186 static int
187 aac_send_fib(struct aac_softstate *softs, struct aac_cmd *acp)
188 {
189 	int rval;
190 
191 	acp->flags |= AAC_CMD_NO_CB | AAC_CMD_SYNC;
192 	acp->ac_comp = aac_ioctl_complete;
193 
194 	mutex_enter(&softs->io_lock);
195 	if (softs->state & AAC_STATE_DEAD) {
196 		mutex_exit(&softs->io_lock);
197 		return (ENXIO);
198 	}
199 
200 	rval = aac_do_io(softs, acp);
201 	if (rval == TRAN_ACCEPT) {
202 		rval = 0;
203 	} else if (rval == TRAN_BADPKT) {
204 		AACDB_PRINT(softs, CE_CONT, "User SendFib failed ENXIO");
205 		rval = ENXIO;
206 	} else if (rval == TRAN_BUSY) {
207 		AACDB_PRINT(softs, CE_CONT, "User SendFib failed EBUSY");
208 		rval = EBUSY;
209 	}
210 	mutex_exit(&softs->io_lock);
211 
212 	return (rval);
213 }
214 
215 static int
216 aac_ioctl_send_fib(struct aac_softstate *softs, intptr_t arg, int mode)
217 {
218 	int hbalen;
219 	struct aac_cmd *acp;
220 	struct aac_fib *fibp;
221 	uint16_t fib_command;
222 	uint32_t fib_xfer_state;
223 	uint16_t fib_data_size, fib_size;
224 	uint16_t fib_sender_size;
225 	int rval;
226 
227 	DBCALLED(softs, 2);
228 
229 	/* Copy in FIB header */
230 	hbalen = sizeof (struct aac_cmd) + softs->aac_max_fib_size;
231 	if ((acp = kmem_zalloc(hbalen, KM_NOSLEEP)) == NULL)
232 		return (ENOMEM);
233 
234 	fibp = (struct aac_fib *)(acp + 1);
235 	acp->fibp = fibp;
236 	if (ddi_copyin((void *)arg, fibp,
237 	    sizeof (struct aac_fib_header), mode) != 0) {
238 		rval = EFAULT;
239 		goto finish;
240 	}
241 
242 	fib_xfer_state = LE_32(fibp->Header.XferState);
243 	fib_command = LE_16(fibp->Header.Command);
244 	fib_data_size = LE_16(fibp->Header.Size);
245 	fib_sender_size = LE_16(fibp->Header.SenderSize);
246 
247 	fib_size = fib_data_size + sizeof (struct aac_fib_header);
248 	if (fib_size < fib_sender_size)
249 		fib_size = fib_sender_size;
250 	if (fib_size > softs->aac_max_fib_size) {
251 		rval = EFAULT;
252 		goto finish;
253 	}
254 
255 	/* Copy in FIB data */
256 	if (ddi_copyin(((struct aac_fib *)arg)->data, fibp->data,
257 	    fib_data_size, mode) != 0) {
258 		rval = EFAULT;
259 		goto finish;
260 	}
261 	acp->fib_size = fib_size;
262 	fibp->Header.Size = LE_16(fib_size);
263 
264 	AACDB_PRINT_FIB(softs, fibp);
265 
266 	/* Process FIB */
267 	if (fib_command == TakeABreakPt) {
268 		(void) aac_sync_mbcommand(softs, AAC_BREAKPOINT_REQ,
269 		    0, 0, 0, 0, NULL);
270 		fibp->Header.XferState = LE_32(0);
271 	} else {
272 		ASSERT(!(fib_xfer_state & AAC_FIBSTATE_ASYNC));
273 		fibp->Header.XferState = LE_32(fib_xfer_state | \
274 		    (AAC_FIBSTATE_FROMHOST | AAC_FIBSTATE_REXPECTED));
275 
276 		acp->timeout = AAC_IOCTL_TIMEOUT;
277 		acp->aac_cmd_fib = aac_cmd_fib_copy;
278 		if ((rval = aac_send_fib(softs, acp)) != 0)
279 			goto finish;
280 	}
281 
282 	if (acp->flags & AAC_CMD_ERR) {
283 		AACDB_PRINT(softs, CE_CONT, "FIB data corrupt");
284 		rval = EIO;
285 		goto finish;
286 	}
287 
288 	if (ddi_copyout(fibp, (void *)arg, acp->fib_size, mode) != 0) {
289 		AACDB_PRINT(softs, CE_CONT, "FIB copyout failed");
290 		rval = EFAULT;
291 		goto finish;
292 	}
293 
294 	rval = 0;
295 finish:
296 	kmem_free(acp, hbalen);
297 	return (rval);
298 }
299 
300 static int
301 aac_open_getadapter_fib(struct aac_softstate *softs, intptr_t arg, int mode)
302 {
303 	struct aac_fib_context *fibctx, *ctx;
304 
305 	DBCALLED(softs, 2);
306 
307 	fibctx = kmem_zalloc(sizeof (struct aac_fib_context), KM_NOSLEEP);
308 	if (fibctx == NULL)
309 		return (ENOMEM);
310 
311 	mutex_enter(&softs->aifq_mutex);
312 	/* All elements are already 0, add to queue */
313 	if (softs->fibctx == NULL) {
314 		softs->fibctx = fibctx;
315 	} else {
316 		for (ctx = softs->fibctx; ctx->next; ctx = ctx->next)
317 			;
318 		ctx->next = fibctx;
319 		fibctx->prev = ctx;
320 	}
321 
322 	/* Evaluate unique value */
323 	fibctx->unique = (unsigned long)fibctx & 0xfffffffful;
324 	ctx = softs->fibctx;
325 	while (ctx != fibctx) {
326 		if (ctx->unique == fibctx->unique) {
327 			fibctx->unique++;
328 			ctx = softs->fibctx;
329 		} else {
330 			ctx = ctx->next;
331 		}
332 	}
333 
334 	/* Set ctx_idx to the oldest AIF */
335 	if (softs->aifq_wrap) {
336 		fibctx->ctx_idx = softs->aifq_idx;
337 		fibctx->ctx_filled = 1;
338 	}
339 	mutex_exit(&softs->aifq_mutex);
340 
341 	if (ddi_copyout(&fibctx->unique, (void *)arg,
342 	    sizeof (uint32_t), mode) != 0)
343 		return (EFAULT);
344 
345 	return (0);
346 }
347 
348 static int
349 aac_return_aif(struct aac_softstate *softs,
350     struct aac_fib_context *ctx, caddr_t uptr, int mode)
351 {
352 	int current;
353 
354 	current = ctx->ctx_idx;
355 	if (current == softs->aifq_idx && !ctx->ctx_filled)
356 		return (EAGAIN); /* Empty */
357 	if (ddi_copyout(&softs->aifq[current].d, (void *)uptr,
358 	    sizeof (struct aac_fib), mode) != 0)
359 		return (EFAULT);
360 
361 	ctx->ctx_filled = 0;
362 	ctx->ctx_idx = (current + 1) % AAC_AIFQ_LENGTH;
363 
364 	return (0);
365 }
366 
367 static int
368 aac_next_getadapter_fib(struct aac_softstate *softs, intptr_t arg, int mode)
369 {
370 	union aac_get_adapter_fib_align un;
371 	struct aac_get_adapter_fib *af = &un.d;
372 	struct aac_fib_context *ctx;
373 	int rval;
374 
375 	DBCALLED(softs, 2);
376 
377 	if (ddi_copyin((void *)arg, af, sizeof (*af), mode) != 0)
378 		return (EFAULT);
379 
380 	mutex_enter(&softs->aifq_mutex);
381 	for (ctx = softs->fibctx; ctx; ctx = ctx->next) {
382 		if (af->context == ctx->unique)
383 			break;
384 	}
385 	if (ctx) {
386 #ifdef	_LP64
387 		rval = aac_return_aif(softs, ctx,
388 		    (caddr_t)(uint64_t)af->aif_fib, mode);
389 #else
390 		rval = aac_return_aif(softs, ctx,
391 		    (caddr_t)af->aif_fib, mode);
392 #endif
393 		if (rval == EAGAIN && af->wait) {
394 			AACDB_PRINT(softs, CE_NOTE,
395 			    "aac_next_getadapter_fib(): waiting for AIF");
396 			rval = cv_wait_sig(&softs->aifv, &softs->aifq_mutex);
397 			if (rval > 0) {
398 #ifdef	_LP64
399 				rval = aac_return_aif(softs, ctx,
400 				    (caddr_t)(uint64_t)af->aif_fib, mode);
401 #else
402 				rval = aac_return_aif(softs, ctx,
403 				    (caddr_t)af->aif_fib, mode);
404 #endif
405 			} else {
406 				rval = EINTR;
407 			}
408 		}
409 	} else {
410 		rval = EFAULT;
411 	}
412 	mutex_exit(&softs->aifq_mutex);
413 
414 	return (rval);
415 }
416 
417 static int
418 aac_close_getadapter_fib(struct aac_softstate *softs, intptr_t arg)
419 {
420 	struct aac_fib_context *ctx;
421 
422 	DBCALLED(softs, 2);
423 
424 	mutex_enter(&softs->aifq_mutex);
425 	for (ctx = softs->fibctx; ctx; ctx = ctx->next) {
426 		if (ctx->unique != (uint32_t)arg)
427 			continue;
428 
429 		if (ctx == softs->fibctx)
430 			softs->fibctx = ctx->next;
431 		else
432 			ctx->prev->next = ctx->next;
433 		if (ctx->next)
434 			ctx->next->prev = ctx->prev;
435 		break;
436 	}
437 	mutex_exit(&softs->aifq_mutex);
438 	if (ctx)
439 		kmem_free(ctx, sizeof (struct aac_fib_context));
440 
441 	return (0);
442 }
443 
444 /*
445  * The following function comes from Adaptec:
446  *
447  * SRB is required for the new management tools
448  * Note: SRB passed down from IOCTL is always in CPU endianness.
449  */
450 static int
451 aac_send_raw_srb(struct aac_softstate *softs, dev_t dev, intptr_t arg, int mode)
452 {
453 	struct aac_cmd *acp;
454 	struct aac_fib *fibp;
455 	struct aac_srb *srb;
456 	uint32_t usr_fib_size;
457 	uint32_t srb_sgcount;
458 	struct aac_umem_sge *usgt = NULL;
459 	struct aac_umem_sge *usge;
460 	ddi_umem_cookie_t cookie;
461 	int umem_flags = 0;
462 	int direct = 0;
463 	int locked = 0;
464 	caddr_t addrlo = (caddr_t)-1;
465 	caddr_t addrhi = 0;
466 	struct aac_sge *sge, *sge0;
467 	int sg64;
468 	int rval;
469 
470 	DBCALLED(softs, 2);
471 
472 	/* Read srb size */
473 	if (ddi_copyin(&((struct aac_srb *)arg)->count, &usr_fib_size,
474 	    sizeof (uint32_t), mode) != 0)
475 		return (EFAULT);
476 	if (usr_fib_size > (softs->aac_max_fib_size - \
477 	    sizeof (struct aac_fib_header)))
478 		return (EINVAL);
479 
480 	if ((acp = kmem_zalloc(sizeof (struct aac_cmd) + usr_fib_size + \
481 	    sizeof (struct aac_fib_header), KM_NOSLEEP)) == NULL)
482 		return (ENOMEM);
483 
484 	acp->fibp = (struct aac_fib *)(acp + 1);
485 	fibp = acp->fibp;
486 	srb = (struct aac_srb *)fibp->data;
487 
488 	/* Copy in srb */
489 	if (ddi_copyin((void *)arg, srb, usr_fib_size, mode) != 0) {
490 		rval = EFAULT;
491 		goto finish;
492 	}
493 
494 	srb_sgcount = srb->sg.SgCount; /* No endianness conversion needed */
495 	if (srb_sgcount == 0)
496 		goto send_fib;
497 
498 	/* Check FIB size */
499 	if (usr_fib_size == (sizeof (struct aac_srb) + \
500 	    srb_sgcount * sizeof (struct aac_sg_entry64) - \
501 	    sizeof (struct aac_sg_entry))) {
502 		sg64 = 1;
503 	} else if (usr_fib_size == (sizeof (struct aac_srb) + \
504 	    (srb_sgcount - 1) * sizeof (struct aac_sg_entry))) {
505 		sg64 = 0;
506 	} else {
507 		rval = EINVAL;
508 		goto finish;
509 	}
510 
511 	/* Read user SG table */
512 	if ((usgt = kmem_zalloc(sizeof (struct aac_umem_sge) * srb_sgcount,
513 	    KM_NOSLEEP)) == NULL) {
514 		rval = ENOMEM;
515 		goto finish;
516 	}
517 	for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
518 		if (sg64) {
519 			struct aac_sg_entry64 *sg64p =
520 			    (struct aac_sg_entry64 *)srb->sg.SgEntry;
521 
522 			usge->bcount = sg64p->SgByteCount;
523 			usge->addr = (caddr_t)
524 #ifndef _LP64
525 			    (uint32_t)
526 #endif
527 			    sg64p->SgAddress;
528 		} else {
529 			struct aac_sg_entry *sgp = srb->sg.SgEntry;
530 
531 			usge->bcount = sgp->SgByteCount;
532 			usge->addr = (caddr_t)
533 #ifdef _LP64
534 			    (uint64_t)
535 #endif
536 			    sgp->SgAddress;
537 		}
538 		acp->bcount += usge->bcount;
539 		if (usge->addr < addrlo)
540 			addrlo = usge->addr;
541 		if ((usge->addr + usge->bcount) > addrhi)
542 			addrhi = usge->addr + usge->bcount;
543 	}
544 	if (acp->bcount > softs->buf_dma_attr.dma_attr_maxxfer) {
545 		AACDB_PRINT(softs, CE_NOTE,
546 		    "large srb xfer size received %d\n", acp->bcount);
547 		rval = EINVAL;
548 		goto finish;
549 	}
550 
551 	/* Lock user buffers */
552 	if (srb->flags & SRB_DataIn) {
553 		umem_flags |= DDI_UMEMLOCK_READ;
554 		direct |= B_READ;
555 	}
556 	if (srb->flags & SRB_DataOut) {
557 		umem_flags |= DDI_UMEMLOCK_WRITE;
558 		direct |= B_WRITE;
559 	}
560 	addrlo = (caddr_t)((uintptr_t)addrlo & (uintptr_t)PAGEMASK);
561 	rval = ddi_umem_lock(addrlo, (((size_t)addrhi + PAGEOFFSET) & \
562 	    PAGEMASK) - (size_t)addrlo, umem_flags, &cookie);
563 	if (rval != 0) {
564 		AACDB_PRINT(softs, CE_NOTE, "ddi_umem_lock failed: %d",
565 		    rval);
566 		goto finish;
567 	}
568 	locked = 1;
569 
570 	/* Allocate DMA for user buffers */
571 	for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
572 		struct buf *bp;
573 
574 		bp = ddi_umem_iosetup(cookie, usge->addr - addrlo,
575 		    usge->bcount, direct, dev, 0, NULL, DDI_UMEM_SLEEP);
576 		if (bp == NULL) {
577 			AACDB_PRINT(softs, CE_NOTE, "ddi_umem_iosetup failed");
578 			rval = EFAULT;
579 			goto finish;
580 		}
581 		if (aac_cmd_dma_alloc(softs, &usge->acp, bp, 0, NULL_FUNC,
582 		    0) != AACOK) {
583 			rval = EFAULT;
584 			goto finish;
585 		}
586 		acp->left_cookien += usge->acp.left_cookien;
587 		if (acp->left_cookien > softs->aac_sg_tablesize) {
588 			AACDB_PRINT(softs, CE_NOTE, "large cookiec received %d",
589 			    acp->left_cookien);
590 			rval = EINVAL;
591 			goto finish;
592 		}
593 	}
594 
595 	/* Construct aac cmd SG table */
596 	if ((sge = kmem_zalloc(sizeof (struct aac_sge) * acp->left_cookien,
597 	    KM_NOSLEEP)) == NULL) {
598 		rval = ENOMEM;
599 		goto finish;
600 	}
601 	acp->sgt = sge;
602 	for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
603 		for (sge0 = usge->acp.sgt;
604 		    sge0 < &usge->acp.sgt[usge->acp.left_cookien];
605 		    sge0++, sge++)
606 			*sge = *sge0;
607 	}
608 
609 send_fib:
610 	acp->cmdlen = srb->cdb_size;
611 	acp->timeout = srb->timeout;
612 
613 	/* Send FIB command */
614 	AACDB_PRINT_FIB(softs, fibp);
615 	acp->aac_cmd_fib = softs->aac_cmd_fib_scsi;
616 	if ((rval = aac_send_fib(softs, acp)) != 0)
617 		goto finish;
618 
619 	/* Status struct */
620 	if (ddi_copyout((struct aac_srb_reply *)fibp->data,
621 	    ((uint8_t *)arg + usr_fib_size),
622 	    sizeof (struct aac_srb_reply), mode) != 0) {
623 		rval = EFAULT;
624 		goto finish;
625 	}
626 
627 	rval = 0;
628 finish:
629 	if (acp->sgt)
630 		kmem_free(acp->sgt, sizeof (struct aac_sge) * \
631 		    acp->left_cookien);
632 	if (usgt) {
633 		for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
634 			if (usge->acp.sgt)
635 				kmem_free(usge->acp.sgt,
636 				    sizeof (struct aac_sge) * \
637 				    usge->acp.left_cookien);
638 			aac_free_dmamap(&usge->acp);
639 			if (usge->acp.bp)
640 				freerbuf(usge->acp.bp);
641 		}
642 		kmem_free(usgt, sizeof (struct aac_umem_sge) * srb_sgcount);
643 	}
644 	if (locked)
645 		ddi_umem_unlock(cookie);
646 	kmem_free(acp, sizeof (struct aac_cmd) + usr_fib_size + \
647 	    sizeof (struct aac_fib_header));
648 	return (rval);
649 }
650 
651 /*ARGSUSED*/
652 static int
653 aac_get_pci_info(struct aac_softstate *softs, intptr_t arg, int mode)
654 {
655 	union aac_pci_info_align un;
656 	struct aac_pci_info *resp = &un.d;
657 
658 	DBCALLED(softs, 2);
659 
660 	resp->bus = 0;
661 	resp->slot = 0;
662 
663 	if (ddi_copyout(resp, (void *)arg,
664 	    sizeof (struct aac_pci_info), mode) != 0)
665 		return (EFAULT);
666 	return (0);
667 }
668 
669 static int
670 aac_query_disk(struct aac_softstate *softs, intptr_t arg, int mode)
671 {
672 	union aac_query_disk_align un;
673 	struct aac_query_disk *qdisk = &un.d;
674 	struct aac_container *dvp;
675 
676 	DBCALLED(softs, 2);
677 
678 	if (ddi_copyin((void *)arg, qdisk, sizeof (*qdisk), mode) != 0)
679 		return (EFAULT);
680 
681 	if (qdisk->container_no == -1) {
682 		qdisk->container_no = qdisk->target * 16 + qdisk->lun;
683 	} else if (qdisk->bus == -1 && qdisk->target == -1 &&
684 	    qdisk->lun == -1) {
685 		if (qdisk->container_no >= AAC_MAX_CONTAINERS)
686 			return (EINVAL);
687 		qdisk->bus = 0;
688 		qdisk->target = (qdisk->container_no & 0xf);
689 		qdisk->lun = (qdisk->container_no >> 4);
690 	} else {
691 		return (EINVAL);
692 	}
693 
694 	mutex_enter(&softs->io_lock);
695 	dvp = &softs->containers[qdisk->container_no];
696 	qdisk->valid = dvp->valid;
697 	qdisk->locked = dvp->locked;
698 	qdisk->deleted = dvp->deleted;
699 	mutex_exit(&softs->io_lock);
700 
701 	if (ddi_copyout(qdisk, (void *)arg, sizeof (*qdisk), mode) != 0)
702 		return (EFAULT);
703 	return (0);
704 }
705 
706 static int
707 aac_delete_disk(struct aac_softstate *softs, intptr_t arg, int mode)
708 {
709 	union aac_delete_disk_align un;
710 	struct aac_delete_disk *ddisk = &un.d;
711 	struct aac_container *dvp;
712 	int rval = 0;
713 
714 	DBCALLED(softs, 2);
715 
716 	if (ddi_copyin((void *)arg, ddisk, sizeof (*ddisk), mode) != 0)
717 		return (EFAULT);
718 
719 	if (ddisk->container_no >= AAC_MAX_CONTAINERS)
720 		return (EINVAL);
721 
722 	mutex_enter(&softs->io_lock);
723 	dvp = &softs->containers[ddisk->container_no];
724 	/*
725 	 * We don't trust the userland to tell us when to delete
726 	 * a container, rather we rely on an AIF coming from the
727 	 * controller.
728 	 */
729 	if (dvp->valid) {
730 		if (dvp->locked)
731 			rval = EBUSY;
732 	}
733 	mutex_exit(&softs->io_lock);
734 
735 	return (rval);
736 }
737 
738 /*
739  * The following function comes from Adaptec to support creation of arrays
740  * bigger than 2TB.
741  */
742 static int
743 aac_supported_features(struct aac_softstate *softs, intptr_t arg, int mode)
744 {
745 	union aac_features_align un;
746 	struct aac_features *f = &un.d;
747 
748 	DBCALLED(softs, 2);
749 
750 	if (ddi_copyin((void *)arg, f, sizeof (*f), mode) != 0)
751 		return (EFAULT);
752 
753 	/*
754 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
755 	 * ALL zero in the featuresState, the driver will return the current
756 	 * state of all the supported features, the data field will not be
757 	 * valid.
758 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
759 	 * a specific bit set in the featuresState, the driver will return the
760 	 * current state of this specific feature and whatever data that are
761 	 * associated with the feature in the data field or perform whatever
762 	 * action needed indicates in the data field.
763 	 */
764 	if (f->feat.fValue == 0) {
765 		f->feat.fBits.largeLBA =
766 		    (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
767 		/* TODO: In the future, add other features state here as well */
768 	} else {
769 		if (f->feat.fBits.largeLBA)
770 			f->feat.fBits.largeLBA =
771 			    (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
772 		/* TODO: Add other features state and data in the future */
773 	}
774 
775 	if (ddi_copyout(f, (void *)arg, sizeof (*f), mode) != 0)
776 		return (EFAULT);
777 	return (0);
778 }
779