xref: /titanic_52/usr/src/uts/common/io/aac/aac_ioctl.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 			uint64_t sgaddr = ((struct aac_sg_entry64 *) \
520 			    srb->sg.SgEntry)->SgAddress;
521 
522 			usge->bcount = ((struct aac_sg_entry64 *)srb-> \
523 			    sg.SgEntry)->SgByteCount;
524 			if (sgaddr > 0xffffffffull ||
525 			    (sgaddr + usge->bcount - 1) > 0xffffffffull) {
526 #ifndef _LP64
527 				if (!(softs->flags & AAC_FLAGS_SG_64BIT))
528 #endif
529 				{
530 					rval = EINVAL;
531 					AACDB_PRINT(softs, CE_NOTE,
532 					    "64-bit addresses not supported");
533 					goto finish;
534 				}
535 			}
536 			usge->addr = (caddr_t)
537 #ifndef _LP64
538 			    (uint32_t)
539 #endif
540 			    sgaddr;
541 		} else {
542 			usge->bcount = srb->sg.SgEntry->SgByteCount;
543 			usge->addr = (caddr_t)
544 #ifdef _LP64
545 			    (uint64_t)
546 #endif
547 			    srb->sg.SgEntry->SgAddress;
548 		}
549 		acp->bcount += usge->bcount;
550 		if (usge->addr < addrlo)
551 			addrlo = usge->addr;
552 		if ((usge->addr + usge->bcount) > addrhi)
553 			addrhi = usge->addr + usge->bcount;
554 	}
555 	if (acp->bcount > softs->buf_dma_attr.dma_attr_maxxfer) {
556 		AACDB_PRINT(softs, CE_NOTE,
557 		    "large srb xfer size received %d\n", acp->bcount);
558 		rval = EINVAL;
559 		goto finish;
560 	}
561 
562 	/* Lock user buffers */
563 	if (srb->flags & SRB_DataIn) {
564 		umem_flags |= DDI_UMEMLOCK_READ;
565 		direct |= B_READ;
566 	}
567 	if (srb->flags & SRB_DataOut) {
568 		umem_flags |= DDI_UMEMLOCK_WRITE;
569 		direct |= B_WRITE;
570 	}
571 	addrlo = (caddr_t)((uintptr_t)addrlo & (uintptr_t)PAGEMASK);
572 	rval = ddi_umem_lock(addrlo, (((size_t)addrhi + PAGEOFFSET) & \
573 	    PAGEMASK) - (size_t)addrlo, umem_flags, &cookie);
574 	if (rval != 0) {
575 		AACDB_PRINT(softs, CE_NOTE, "ddi_umem_lock failed: %d",
576 		    rval);
577 		goto finish;
578 	}
579 	locked = 1;
580 
581 	/* Allocate DMA for user buffers */
582 	for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
583 		struct buf *bp;
584 
585 		bp = ddi_umem_iosetup(cookie, usge->addr - addrlo,
586 		    usge->bcount, direct, dev, 0, NULL, DDI_UMEM_SLEEP);
587 		if (bp == NULL) {
588 			AACDB_PRINT(softs, CE_NOTE, "ddi_umem_iosetup failed");
589 			rval = EFAULT;
590 			goto finish;
591 		}
592 		if (aac_cmd_dma_alloc(softs, &usge->acp, bp, 0, NULL_FUNC,
593 		    0) != AACOK) {
594 			rval = EFAULT;
595 			goto finish;
596 		}
597 		acp->left_cookien += usge->acp.left_cookien;
598 		if (acp->left_cookien > softs->aac_sg_tablesize) {
599 			AACDB_PRINT(softs, CE_NOTE, "large cookiec received %d",
600 			    acp->left_cookien);
601 			rval = EINVAL;
602 			goto finish;
603 		}
604 	}
605 
606 	/* Construct aac cmd SG table */
607 	if ((sge = kmem_zalloc(sizeof (struct aac_sge) * acp->left_cookien,
608 	    KM_NOSLEEP)) == NULL) {
609 		rval = ENOMEM;
610 		goto finish;
611 	}
612 	acp->sgt = sge;
613 	for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
614 		for (sge0 = usge->acp.sgt;
615 		    sge0 < &usge->acp.sgt[usge->acp.left_cookien];
616 		    sge0++, sge++)
617 			*sge = *sge0;
618 	}
619 
620 send_fib:
621 	acp->cmdlen = srb->cdb_size;
622 	acp->timeout = srb->timeout;
623 
624 	/* Send FIB command */
625 	AACDB_PRINT_FIB(softs, fibp);
626 	acp->aac_cmd_fib = softs->aac_cmd_fib_scsi;
627 	if ((rval = aac_send_fib(softs, acp)) != 0)
628 		goto finish;
629 
630 	/* Status struct */
631 	if (ddi_copyout((struct aac_srb_reply *)fibp->data,
632 	    ((uint8_t *)arg + usr_fib_size),
633 	    sizeof (struct aac_srb_reply), mode) != 0) {
634 		rval = EFAULT;
635 		goto finish;
636 	}
637 
638 	rval = 0;
639 finish:
640 	if (acp->sgt)
641 		kmem_free(acp->sgt, sizeof (struct aac_sge) * \
642 		    acp->left_cookien);
643 	if (usgt) {
644 		for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
645 			if (usge->acp.sgt)
646 				kmem_free(usge->acp.sgt,
647 				    sizeof (struct aac_sge) * \
648 				    usge->acp.left_cookien);
649 			aac_free_dmamap(&usge->acp);
650 			if (usge->acp.bp)
651 				freerbuf(usge->acp.bp);
652 		}
653 		kmem_free(usgt, sizeof (struct aac_umem_sge) * srb_sgcount);
654 	}
655 	if (locked)
656 		ddi_umem_unlock(cookie);
657 	kmem_free(acp, sizeof (struct aac_cmd) + usr_fib_size + \
658 	    sizeof (struct aac_fib_header));
659 	return (rval);
660 }
661 
662 /*ARGSUSED*/
663 static int
664 aac_get_pci_info(struct aac_softstate *softs, intptr_t arg, int mode)
665 {
666 	union aac_pci_info_align un;
667 	struct aac_pci_info *resp = &un.d;
668 
669 	DBCALLED(softs, 2);
670 
671 	resp->bus = 0;
672 	resp->slot = 0;
673 
674 	if (ddi_copyout(resp, (void *)arg,
675 	    sizeof (struct aac_pci_info), mode) != 0)
676 		return (EFAULT);
677 	return (0);
678 }
679 
680 static int
681 aac_query_disk(struct aac_softstate *softs, intptr_t arg, int mode)
682 {
683 	union aac_query_disk_align un;
684 	struct aac_query_disk *qdisk = &un.d;
685 	struct aac_container *dvp;
686 
687 	DBCALLED(softs, 2);
688 
689 	if (ddi_copyin((void *)arg, qdisk, sizeof (*qdisk), mode) != 0)
690 		return (EFAULT);
691 
692 	if (qdisk->container_no == -1) {
693 		qdisk->container_no = qdisk->target * 16 + qdisk->lun;
694 	} else if (qdisk->bus == -1 && qdisk->target == -1 &&
695 	    qdisk->lun == -1) {
696 		if (qdisk->container_no >= AAC_MAX_CONTAINERS)
697 			return (EINVAL);
698 		qdisk->bus = 0;
699 		qdisk->target = (qdisk->container_no & 0xf);
700 		qdisk->lun = (qdisk->container_no >> 4);
701 	} else {
702 		return (EINVAL);
703 	}
704 
705 	mutex_enter(&softs->io_lock);
706 	dvp = &softs->containers[qdisk->container_no];
707 	qdisk->valid = dvp->valid;
708 	qdisk->locked = dvp->locked;
709 	qdisk->deleted = dvp->deleted;
710 	mutex_exit(&softs->io_lock);
711 
712 	if (ddi_copyout(qdisk, (void *)arg, sizeof (*qdisk), mode) != 0)
713 		return (EFAULT);
714 	return (0);
715 }
716 
717 static int
718 aac_delete_disk(struct aac_softstate *softs, intptr_t arg, int mode)
719 {
720 	union aac_delete_disk_align un;
721 	struct aac_delete_disk *ddisk = &un.d;
722 	struct aac_container *dvp;
723 	int rval = 0;
724 
725 	DBCALLED(softs, 2);
726 
727 	if (ddi_copyin((void *)arg, ddisk, sizeof (*ddisk), mode) != 0)
728 		return (EFAULT);
729 
730 	if (ddisk->container_no >= AAC_MAX_CONTAINERS)
731 		return (EINVAL);
732 
733 	mutex_enter(&softs->io_lock);
734 	dvp = &softs->containers[ddisk->container_no];
735 	/*
736 	 * We don't trust the userland to tell us when to delete
737 	 * a container, rather we rely on an AIF coming from the
738 	 * controller.
739 	 */
740 	if (dvp->valid) {
741 		if (dvp->locked)
742 			rval = EBUSY;
743 	}
744 	mutex_exit(&softs->io_lock);
745 
746 	return (rval);
747 }
748 
749 /*
750  * The following function comes from Adaptec to support creation of arrays
751  * bigger than 2TB.
752  */
753 static int
754 aac_supported_features(struct aac_softstate *softs, intptr_t arg, int mode)
755 {
756 	union aac_features_align un;
757 	struct aac_features *f = &un.d;
758 
759 	DBCALLED(softs, 2);
760 
761 	if (ddi_copyin((void *)arg, f, sizeof (*f), mode) != 0)
762 		return (EFAULT);
763 
764 	/*
765 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
766 	 * ALL zero in the featuresState, the driver will return the current
767 	 * state of all the supported features, the data field will not be
768 	 * valid.
769 	 * When the management driver receives FSACTL_GET_FEATURES ioctl with
770 	 * a specific bit set in the featuresState, the driver will return the
771 	 * current state of this specific feature and whatever data that are
772 	 * associated with the feature in the data field or perform whatever
773 	 * action needed indicates in the data field.
774 	 */
775 	if (f->feat.fValue == 0) {
776 		f->feat.fBits.largeLBA =
777 		    (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
778 		/* TODO: In the future, add other features state here as well */
779 	} else {
780 		if (f->feat.fBits.largeLBA)
781 			f->feat.fBits.largeLBA =
782 			    (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
783 		/* TODO: Add other features state and data in the future */
784 	}
785 
786 	if (ddi_copyout(f, (void *)arg, sizeof (*f), mode) != 0)
787 		return (EFAULT);
788 	return (0);
789 }
790