15678Spl196000 /*
2*12408SZhongyan.Gu@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
35678Spl196000 */
45678Spl196000
55678Spl196000 /*
65678Spl196000 * Copyright 2005-06 Adaptec, Inc.
75678Spl196000 * Copyright (c) 2005-06 Adaptec Inc., Achim Leubner
85678Spl196000 * Copyright (c) 2000 Michael Smith
95678Spl196000 * Copyright (c) 2001 Scott Long
105678Spl196000 * Copyright (c) 2000 BSDi
115678Spl196000 * All rights reserved.
125678Spl196000 *
135678Spl196000 * Redistribution and use in source and binary forms, with or without
145678Spl196000 * modification, are permitted provided that the following conditions
155678Spl196000 * are met:
165678Spl196000 * 1. Redistributions of source code must retain the above copyright
175678Spl196000 * notice, this list of conditions and the following disclaimer.
185678Spl196000 * 2. Redistributions in binary form must reproduce the above copyright
195678Spl196000 * notice, this list of conditions and the following disclaimer in the
205678Spl196000 * documentation and/or other materials provided with the distribution.
215678Spl196000 *
225678Spl196000 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
235678Spl196000 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
245678Spl196000 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
255678Spl196000 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
265678Spl196000 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
275678Spl196000 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
285678Spl196000 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
295678Spl196000 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
305678Spl196000 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
315678Spl196000 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
325678Spl196000 * SUCH DAMAGE.
335678Spl196000 */
345678Spl196000 #include <sys/modctl.h>
355678Spl196000 #include <sys/conf.h>
365678Spl196000 #include <sys/cmn_err.h>
375678Spl196000 #include <sys/ddi.h>
385678Spl196000 #include <sys/devops.h>
395678Spl196000 #include <sys/pci.h>
405678Spl196000 #include <sys/types.h>
415678Spl196000 #include <sys/ddidmareq.h>
425678Spl196000 #include <sys/scsi/scsi.h>
435678Spl196000 #include <sys/ksynch.h>
445678Spl196000 #include <sys/sunddi.h>
455678Spl196000 #include <sys/byteorder.h>
465678Spl196000 #include <sys/kmem.h>
475678Spl196000 #include "aac_regs.h"
485678Spl196000 #include "aac.h"
495678Spl196000 #include "aac_ioctl.h"
505678Spl196000
515678Spl196000 struct aac_umem_sge {
525678Spl196000 uint32_t bcount;
535678Spl196000 caddr_t addr;
545678Spl196000 struct aac_cmd acp;
555678Spl196000 };
565678Spl196000
575678Spl196000 /*
585678Spl196000 * External functions
595678Spl196000 */
605678Spl196000 extern int aac_sync_mbcommand(struct aac_softstate *, uint32_t, uint32_t,
615678Spl196000 uint32_t, uint32_t, uint32_t, uint32_t *);
625678Spl196000 extern int aac_cmd_dma_alloc(struct aac_softstate *, struct aac_cmd *,
635678Spl196000 struct buf *, int, int (*)(), caddr_t);
645678Spl196000 extern void aac_free_dmamap(struct aac_cmd *);
655678Spl196000 extern int aac_do_io(struct aac_softstate *, struct aac_cmd *);
665678Spl196000 extern void aac_cmd_fib_copy(struct aac_softstate *, struct aac_cmd *);
675678Spl196000 extern void aac_ioctl_complete(struct aac_softstate *, struct aac_cmd *);
6811964SXin.Chen@Sun.COM extern int aac_return_aif_wait(struct aac_softstate *, struct aac_fib_context *,
6911964SXin.Chen@Sun.COM struct aac_fib **);
7011964SXin.Chen@Sun.COM extern int aac_return_aif(struct aac_softstate *, struct aac_fib_context *,
7111964SXin.Chen@Sun.COM struct aac_fib **);
725678Spl196000
735678Spl196000 extern ddi_device_acc_attr_t aac_acc_attr;
745678Spl196000 extern int aac_check_dma_handle(ddi_dma_handle_t);
755678Spl196000
765678Spl196000 /*
775678Spl196000 * IOCTL command handling functions
785678Spl196000 */
795678Spl196000 static int aac_check_revision(struct aac_softstate *, intptr_t, int);
805678Spl196000 static int aac_ioctl_send_fib(struct aac_softstate *, intptr_t, int);
815678Spl196000 static int aac_open_getadapter_fib(struct aac_softstate *, intptr_t, int);
825678Spl196000 static int aac_next_getadapter_fib(struct aac_softstate *, intptr_t, int);
835678Spl196000 static int aac_close_getadapter_fib(struct aac_softstate *, intptr_t);
845678Spl196000 static int aac_send_raw_srb(struct aac_softstate *, dev_t, intptr_t, int);
855678Spl196000 static int aac_get_pci_info(struct aac_softstate *, intptr_t, int);
865678Spl196000 static int aac_query_disk(struct aac_softstate *, intptr_t, int);
875678Spl196000 static int aac_delete_disk(struct aac_softstate *, intptr_t, int);
885678Spl196000 static int aac_supported_features(struct aac_softstate *, intptr_t, int);
895678Spl196000
905678Spl196000 /*
915678Spl196000 * Warlock directives
925678Spl196000 */
935678Spl196000 _NOTE(SCHEME_PROTECTS_DATA("unique to each handling function", aac_features
945678Spl196000 aac_pci_info aac_query_disk aac_revision aac_umem_sge))
955678Spl196000
965678Spl196000 int
aac_do_ioctl(struct aac_softstate * softs,dev_t dev,int cmd,intptr_t arg,int mode)975678Spl196000 aac_do_ioctl(struct aac_softstate *softs, dev_t dev, int cmd, intptr_t arg,
985678Spl196000 int mode)
995678Spl196000 {
1005678Spl196000 int status;
1015678Spl196000
1025678Spl196000 switch (cmd) {
1035678Spl196000 case FSACTL_MINIPORT_REV_CHECK:
1045678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_MINIPORT_REV_CHECK");
1055678Spl196000 status = aac_check_revision(softs, arg, mode);
1065678Spl196000 break;
1075678Spl196000 case FSACTL_SENDFIB:
1085678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_LARGE_FIB");
1095678Spl196000 goto send_fib;
1105678Spl196000 case FSACTL_SEND_LARGE_FIB:
1115678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_LARGE_FIB");
1125678Spl196000 send_fib:
1135678Spl196000 status = aac_ioctl_send_fib(softs, arg, mode);
1145678Spl196000 break;
1155678Spl196000 case FSACTL_OPEN_GET_ADAPTER_FIB:
1165678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_OPEN_GET_ADAPTER_FIB");
1175678Spl196000 status = aac_open_getadapter_fib(softs, arg, mode);
1185678Spl196000 break;
1195678Spl196000 case FSACTL_GET_NEXT_ADAPTER_FIB:
1205678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_GET_NEXT_ADAPTER_FIB");
1215678Spl196000 status = aac_next_getadapter_fib(softs, arg, mode);
1225678Spl196000 break;
1235678Spl196000 case FSACTL_CLOSE_GET_ADAPTER_FIB:
1245678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_CLOSE_GET_ADAPTER_FIB");
1255678Spl196000 status = aac_close_getadapter_fib(softs, arg);
1265678Spl196000 break;
1275678Spl196000 case FSACTL_SEND_RAW_SRB:
1285678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_SEND_RAW_SRB");
1295678Spl196000 status = aac_send_raw_srb(softs, dev, arg, mode);
1305678Spl196000 break;
1315678Spl196000 case FSACTL_GET_PCI_INFO:
1325678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_GET_PCI_INFO");
1335678Spl196000 status = aac_get_pci_info(softs, arg, mode);
1345678Spl196000 break;
1355678Spl196000 case FSACTL_QUERY_DISK:
1365678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_QUERY_DISK");
1375678Spl196000 status = aac_query_disk(softs, arg, mode);
1385678Spl196000 break;
1395678Spl196000 case FSACTL_DELETE_DISK:
1405678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_DELETE_DISK");
1415678Spl196000 status = aac_delete_disk(softs, arg, mode);
1425678Spl196000 break;
1435678Spl196000 case FSACTL_GET_FEATURES:
1445678Spl196000 AACDB_PRINT_IOCTL(softs, "FSACTL_GET_FEATURES");
1455678Spl196000 status = aac_supported_features(softs, arg, mode);
1465678Spl196000 break;
1475678Spl196000 default:
1485678Spl196000 status = ENOTTY;
1495678Spl196000 AACDB_PRINT(softs, CE_WARN,
1505678Spl196000 "!IOCTL cmd 0x%x not supported", cmd);
1515678Spl196000 break;
1525678Spl196000 }
1535678Spl196000
1545678Spl196000 return (status);
1555678Spl196000 }
1565678Spl196000
1575678Spl196000 /*ARGSUSED*/
1585678Spl196000 static int
aac_check_revision(struct aac_softstate * softs,intptr_t arg,int mode)1595678Spl196000 aac_check_revision(struct aac_softstate *softs, intptr_t arg, int mode)
1605678Spl196000 {
1615678Spl196000 union aac_revision_align un;
1625678Spl196000 struct aac_revision *aac_rev = &un.d;
1635678Spl196000
1645678Spl196000 DBCALLED(softs, 2);
1655678Spl196000
1665678Spl196000 /* Copyin the revision struct from userspace */
1675678Spl196000 if (ddi_copyin((void *)arg, aac_rev,
1685678Spl196000 sizeof (struct aac_revision), mode) != 0)
1695678Spl196000 return (EFAULT);
1705678Spl196000
1715678Spl196000 /* Doctor up the response struct */
1725678Spl196000 aac_rev->compat = 1;
1735678Spl196000 aac_rev->version =
1745678Spl196000 ((uint32_t)AAC_DRIVER_MAJOR_VERSION << 24) |
1755678Spl196000 ((uint32_t)AAC_DRIVER_MINOR_VERSION << 16) |
1765678Spl196000 ((uint32_t)AAC_DRIVER_TYPE << 8) |
1775678Spl196000 ((uint32_t)AAC_DRIVER_BUGFIX_LEVEL);
1785678Spl196000 aac_rev->build = (uint32_t)AAC_DRIVER_BUILD;
1795678Spl196000
1805678Spl196000 if (ddi_copyout(aac_rev, (void *)arg,
1815678Spl196000 sizeof (struct aac_revision), mode) != 0)
1825678Spl196000 return (EFAULT);
1835678Spl196000
1845678Spl196000 return (0);
1855678Spl196000 }
1865678Spl196000
1875678Spl196000 static int
aac_send_fib(struct aac_softstate * softs,struct aac_cmd * acp)1885678Spl196000 aac_send_fib(struct aac_softstate *softs, struct aac_cmd *acp)
1895678Spl196000 {
1905678Spl196000 int rval;
1915678Spl196000
1925678Spl196000 acp->flags |= AAC_CMD_NO_CB | AAC_CMD_SYNC;
1935678Spl196000 acp->ac_comp = aac_ioctl_complete;
1945678Spl196000
1955678Spl196000 mutex_enter(&softs->io_lock);
1965678Spl196000 if (softs->state & AAC_STATE_DEAD) {
1975678Spl196000 mutex_exit(&softs->io_lock);
1985678Spl196000 return (ENXIO);
1995678Spl196000 }
2005678Spl196000
2015678Spl196000 rval = aac_do_io(softs, acp);
2025678Spl196000 if (rval == TRAN_ACCEPT) {
2035678Spl196000 rval = 0;
2045678Spl196000 } else if (rval == TRAN_BADPKT) {
2055678Spl196000 AACDB_PRINT(softs, CE_CONT, "User SendFib failed ENXIO");
2065678Spl196000 rval = ENXIO;
2075678Spl196000 } else if (rval == TRAN_BUSY) {
2085678Spl196000 AACDB_PRINT(softs, CE_CONT, "User SendFib failed EBUSY");
2095678Spl196000 rval = EBUSY;
2105678Spl196000 }
2115678Spl196000 mutex_exit(&softs->io_lock);
2125678Spl196000
2135678Spl196000 return (rval);
2145678Spl196000 }
2155678Spl196000
2165678Spl196000 static int
aac_ioctl_send_fib(struct aac_softstate * softs,intptr_t arg,int mode)2175678Spl196000 aac_ioctl_send_fib(struct aac_softstate *softs, intptr_t arg, int mode)
2185678Spl196000 {
2195678Spl196000 int hbalen;
2205678Spl196000 struct aac_cmd *acp;
2215678Spl196000 struct aac_fib *fibp;
2225678Spl196000 uint16_t fib_command;
2235678Spl196000 uint32_t fib_xfer_state;
2245678Spl196000 uint16_t fib_data_size, fib_size;
2255678Spl196000 uint16_t fib_sender_size;
2265678Spl196000 int rval;
2275678Spl196000
2285678Spl196000 DBCALLED(softs, 2);
2295678Spl196000
2305678Spl196000 /* Copy in FIB header */
2315678Spl196000 hbalen = sizeof (struct aac_cmd) + softs->aac_max_fib_size;
2325678Spl196000 if ((acp = kmem_zalloc(hbalen, KM_NOSLEEP)) == NULL)
2335678Spl196000 return (ENOMEM);
2345678Spl196000
2355678Spl196000 fibp = (struct aac_fib *)(acp + 1);
2365678Spl196000 acp->fibp = fibp;
2375678Spl196000 if (ddi_copyin((void *)arg, fibp,
2385678Spl196000 sizeof (struct aac_fib_header), mode) != 0) {
2395678Spl196000 rval = EFAULT;
2405678Spl196000 goto finish;
2415678Spl196000 }
2425678Spl196000
2435678Spl196000 fib_xfer_state = LE_32(fibp->Header.XferState);
2445678Spl196000 fib_command = LE_16(fibp->Header.Command);
2455678Spl196000 fib_data_size = LE_16(fibp->Header.Size);
2465678Spl196000 fib_sender_size = LE_16(fibp->Header.SenderSize);
2475678Spl196000
2485678Spl196000 fib_size = fib_data_size + sizeof (struct aac_fib_header);
2495678Spl196000 if (fib_size < fib_sender_size)
2505678Spl196000 fib_size = fib_sender_size;
2515678Spl196000 if (fib_size > softs->aac_max_fib_size) {
2525678Spl196000 rval = EFAULT;
2535678Spl196000 goto finish;
2545678Spl196000 }
2555678Spl196000
2565678Spl196000 /* Copy in FIB data */
2575678Spl196000 if (ddi_copyin(((struct aac_fib *)arg)->data, fibp->data,
2585678Spl196000 fib_data_size, mode) != 0) {
2595678Spl196000 rval = EFAULT;
2605678Spl196000 goto finish;
2615678Spl196000 }
2625678Spl196000 acp->fib_size = fib_size;
2635678Spl196000 fibp->Header.Size = LE_16(fib_size);
2645678Spl196000
2655678Spl196000 /* Process FIB */
2665678Spl196000 if (fib_command == TakeABreakPt) {
2677567SXin.Chen@Sun.COM #ifdef DEBUG
2687567SXin.Chen@Sun.COM if (aac_dbflag_on(softs, AACDB_FLAGS_FIB) &&
2697567SXin.Chen@Sun.COM (softs->debug_fib_flags & AACDB_FLAGS_FIB_IOCTL))
2707567SXin.Chen@Sun.COM aac_printf(softs, CE_NOTE, "FIB> TakeABreakPt, sz=%d",
2717567SXin.Chen@Sun.COM fib_size);
2727567SXin.Chen@Sun.COM #endif
2735678Spl196000 (void) aac_sync_mbcommand(softs, AAC_BREAKPOINT_REQ,
2745678Spl196000 0, 0, 0, 0, NULL);
2755678Spl196000 fibp->Header.XferState = LE_32(0);
2765678Spl196000 } else {
2775678Spl196000 ASSERT(!(fib_xfer_state & AAC_FIBSTATE_ASYNC));
2785678Spl196000 fibp->Header.XferState = LE_32(fib_xfer_state | \
2795678Spl196000 (AAC_FIBSTATE_FROMHOST | AAC_FIBSTATE_REXPECTED));
2805678Spl196000
2815678Spl196000 acp->timeout = AAC_IOCTL_TIMEOUT;
2825678Spl196000 acp->aac_cmd_fib = aac_cmd_fib_copy;
2837567SXin.Chen@Sun.COM #ifdef DEBUG
2847567SXin.Chen@Sun.COM acp->fib_flags = AACDB_FLAGS_FIB_IOCTL;
2857567SXin.Chen@Sun.COM #endif
2865678Spl196000 if ((rval = aac_send_fib(softs, acp)) != 0)
2875678Spl196000 goto finish;
2885678Spl196000 }
2895678Spl196000
2905678Spl196000 if (acp->flags & AAC_CMD_ERR) {
2915678Spl196000 AACDB_PRINT(softs, CE_CONT, "FIB data corrupt");
2925678Spl196000 rval = EIO;
2935678Spl196000 goto finish;
2945678Spl196000 }
2955678Spl196000
2965678Spl196000 if (ddi_copyout(fibp, (void *)arg, acp->fib_size, mode) != 0) {
2975678Spl196000 AACDB_PRINT(softs, CE_CONT, "FIB copyout failed");
2985678Spl196000 rval = EFAULT;
2995678Spl196000 goto finish;
3005678Spl196000 }
3015678Spl196000
3025678Spl196000 rval = 0;
3035678Spl196000 finish:
3045678Spl196000 kmem_free(acp, hbalen);
3055678Spl196000 return (rval);
3065678Spl196000 }
3075678Spl196000
3085678Spl196000 static int
aac_open_getadapter_fib(struct aac_softstate * softs,intptr_t arg,int mode)3095678Spl196000 aac_open_getadapter_fib(struct aac_softstate *softs, intptr_t arg, int mode)
3105678Spl196000 {
31111964SXin.Chen@Sun.COM struct aac_fib_context *fibctx_p, *ctx_p;
3125678Spl196000
3135678Spl196000 DBCALLED(softs, 2);
3145678Spl196000
31511964SXin.Chen@Sun.COM fibctx_p = kmem_zalloc(sizeof (struct aac_fib_context), KM_NOSLEEP);
31611964SXin.Chen@Sun.COM if (fibctx_p == NULL)
3175678Spl196000 return (ENOMEM);
3185678Spl196000
3195678Spl196000 mutex_enter(&softs->aifq_mutex);
3205678Spl196000 /* All elements are already 0, add to queue */
32111964SXin.Chen@Sun.COM if (softs->fibctx_p == NULL) {
32211964SXin.Chen@Sun.COM softs->fibctx_p = fibctx_p;
3235678Spl196000 } else {
32411964SXin.Chen@Sun.COM for (ctx_p = softs->fibctx_p; ctx_p->next; ctx_p = ctx_p->next)
3255678Spl196000 ;
32611964SXin.Chen@Sun.COM ctx_p->next = fibctx_p;
32711964SXin.Chen@Sun.COM fibctx_p->prev = ctx_p;
3285678Spl196000 }
3295678Spl196000
3305678Spl196000 /* Evaluate unique value */
33111964SXin.Chen@Sun.COM fibctx_p->unique = (unsigned long)fibctx_p & 0xfffffffful;
33211964SXin.Chen@Sun.COM ctx_p = softs->fibctx_p;
33311964SXin.Chen@Sun.COM while (ctx_p != fibctx_p) {
33411964SXin.Chen@Sun.COM if (ctx_p->unique == fibctx_p->unique) {
33511964SXin.Chen@Sun.COM fibctx_p->unique++;
33611964SXin.Chen@Sun.COM ctx_p = softs->fibctx_p;
3375678Spl196000 } else {
33811964SXin.Chen@Sun.COM ctx_p = ctx_p->next;
3395678Spl196000 }
3405678Spl196000 }
3415678Spl196000
3425678Spl196000 /* Set ctx_idx to the oldest AIF */
3435678Spl196000 if (softs->aifq_wrap) {
34411964SXin.Chen@Sun.COM fibctx_p->ctx_idx = softs->aifq_idx;
34511964SXin.Chen@Sun.COM fibctx_p->ctx_filled = 1;
3465678Spl196000 }
3475678Spl196000 mutex_exit(&softs->aifq_mutex);
3485678Spl196000
34911964SXin.Chen@Sun.COM if (ddi_copyout(&fibctx_p->unique, (void *)arg,
3505678Spl196000 sizeof (uint32_t), mode) != 0)
3515678Spl196000 return (EFAULT);
3525678Spl196000
3535678Spl196000 return (0);
3545678Spl196000 }
3555678Spl196000
3565678Spl196000 static int
aac_next_getadapter_fib(struct aac_softstate * softs,intptr_t arg,int mode)3575678Spl196000 aac_next_getadapter_fib(struct aac_softstate *softs, intptr_t arg, int mode)
3585678Spl196000 {
3595678Spl196000 union aac_get_adapter_fib_align un;
3605678Spl196000 struct aac_get_adapter_fib *af = &un.d;
36111964SXin.Chen@Sun.COM struct aac_fib_context *ctx_p;
36211964SXin.Chen@Sun.COM struct aac_fib *fibp;
3635678Spl196000 int rval;
3645678Spl196000
3655678Spl196000 DBCALLED(softs, 2);
3665678Spl196000
3675678Spl196000 if (ddi_copyin((void *)arg, af, sizeof (*af), mode) != 0)
3685678Spl196000 return (EFAULT);
3695678Spl196000
3705678Spl196000 mutex_enter(&softs->aifq_mutex);
37111964SXin.Chen@Sun.COM for (ctx_p = softs->fibctx_p; ctx_p; ctx_p = ctx_p->next) {
37211964SXin.Chen@Sun.COM if (af->context == ctx_p->unique)
3735678Spl196000 break;
3745678Spl196000 }
3755678Spl196000 mutex_exit(&softs->aifq_mutex);
3765678Spl196000
37711964SXin.Chen@Sun.COM if (ctx_p) {
37811964SXin.Chen@Sun.COM if (af->wait)
37911964SXin.Chen@Sun.COM rval = aac_return_aif_wait(softs, ctx_p, &fibp);
38011964SXin.Chen@Sun.COM else
38111964SXin.Chen@Sun.COM rval = aac_return_aif(softs, ctx_p, &fibp);
38211964SXin.Chen@Sun.COM }
38311964SXin.Chen@Sun.COM else
38411964SXin.Chen@Sun.COM rval = EFAULT;
38511964SXin.Chen@Sun.COM
38611964SXin.Chen@Sun.COM finish:
38711964SXin.Chen@Sun.COM if (rval == 0) {
38811964SXin.Chen@Sun.COM if (ddi_copyout(fibp,
38911964SXin.Chen@Sun.COM #ifdef _LP64
39011964SXin.Chen@Sun.COM (void *)(uint64_t)af->aif_fib,
39111964SXin.Chen@Sun.COM #else
39211964SXin.Chen@Sun.COM (void *)af->aif_fib,
39311964SXin.Chen@Sun.COM #endif
39411964SXin.Chen@Sun.COM sizeof (struct aac_fib), mode) != 0)
39511964SXin.Chen@Sun.COM rval = EFAULT;
39611964SXin.Chen@Sun.COM }
3975678Spl196000 return (rval);
3985678Spl196000 }
3995678Spl196000
4005678Spl196000 static int
aac_close_getadapter_fib(struct aac_softstate * softs,intptr_t arg)4015678Spl196000 aac_close_getadapter_fib(struct aac_softstate *softs, intptr_t arg)
4025678Spl196000 {
40311964SXin.Chen@Sun.COM struct aac_fib_context *ctx_p;
4045678Spl196000
4055678Spl196000 DBCALLED(softs, 2);
4065678Spl196000
4075678Spl196000 mutex_enter(&softs->aifq_mutex);
40811964SXin.Chen@Sun.COM for (ctx_p = softs->fibctx_p; ctx_p; ctx_p = ctx_p->next) {
40911964SXin.Chen@Sun.COM if (ctx_p->unique != (uint32_t)arg)
4105678Spl196000 continue;
4115678Spl196000
41211964SXin.Chen@Sun.COM if (ctx_p == softs->fibctx_p)
41311964SXin.Chen@Sun.COM softs->fibctx_p = ctx_p->next;
4145678Spl196000 else
41511964SXin.Chen@Sun.COM ctx_p->prev->next = ctx_p->next;
41611964SXin.Chen@Sun.COM if (ctx_p->next)
41711964SXin.Chen@Sun.COM ctx_p->next->prev = ctx_p->prev;
4185678Spl196000 break;
4195678Spl196000 }
4205678Spl196000 mutex_exit(&softs->aifq_mutex);
42111964SXin.Chen@Sun.COM if (ctx_p)
42211964SXin.Chen@Sun.COM kmem_free(ctx_p, sizeof (struct aac_fib_context));
4235678Spl196000
4245678Spl196000 return (0);
4255678Spl196000 }
4265678Spl196000
4275678Spl196000 /*
4285678Spl196000 * The following function comes from Adaptec:
4295678Spl196000 *
4305678Spl196000 * SRB is required for the new management tools
4315678Spl196000 * Note: SRB passed down from IOCTL is always in CPU endianness.
4325678Spl196000 */
4335678Spl196000 static int
aac_send_raw_srb(struct aac_softstate * softs,dev_t dev,intptr_t arg,int mode)4345678Spl196000 aac_send_raw_srb(struct aac_softstate *softs, dev_t dev, intptr_t arg, int mode)
4355678Spl196000 {
4365678Spl196000 struct aac_cmd *acp;
4375678Spl196000 struct aac_fib *fibp;
4385678Spl196000 struct aac_srb *srb;
4395678Spl196000 uint32_t usr_fib_size;
4405678Spl196000 uint32_t srb_sgcount;
4415678Spl196000 struct aac_umem_sge *usgt = NULL;
4425678Spl196000 struct aac_umem_sge *usge;
4435678Spl196000 ddi_umem_cookie_t cookie;
4445678Spl196000 int umem_flags = 0;
4455678Spl196000 int direct = 0;
4465678Spl196000 int locked = 0;
4475678Spl196000 caddr_t addrlo = (caddr_t)-1;
4485678Spl196000 caddr_t addrhi = 0;
4495678Spl196000 struct aac_sge *sge, *sge0;
4505678Spl196000 int sg64;
4515678Spl196000 int rval;
4525678Spl196000
4535678Spl196000 DBCALLED(softs, 2);
4545678Spl196000
4555678Spl196000 /* Read srb size */
4565678Spl196000 if (ddi_copyin(&((struct aac_srb *)arg)->count, &usr_fib_size,
4575678Spl196000 sizeof (uint32_t), mode) != 0)
4585678Spl196000 return (EFAULT);
4595678Spl196000 if (usr_fib_size > (softs->aac_max_fib_size - \
4605678Spl196000 sizeof (struct aac_fib_header)))
4615678Spl196000 return (EINVAL);
4625678Spl196000
4635678Spl196000 if ((acp = kmem_zalloc(sizeof (struct aac_cmd) + usr_fib_size + \
4645678Spl196000 sizeof (struct aac_fib_header), KM_NOSLEEP)) == NULL)
4655678Spl196000 return (ENOMEM);
4665678Spl196000
4675678Spl196000 acp->fibp = (struct aac_fib *)(acp + 1);
4685678Spl196000 fibp = acp->fibp;
4695678Spl196000 srb = (struct aac_srb *)fibp->data;
4705678Spl196000
4715678Spl196000 /* Copy in srb */
4725678Spl196000 if (ddi_copyin((void *)arg, srb, usr_fib_size, mode) != 0) {
4735678Spl196000 rval = EFAULT;
4745678Spl196000 goto finish;
4755678Spl196000 }
4765678Spl196000
4775678Spl196000 srb_sgcount = srb->sg.SgCount; /* No endianness conversion needed */
4785678Spl196000 if (srb_sgcount == 0)
4795678Spl196000 goto send_fib;
4805678Spl196000
4815678Spl196000 /* Check FIB size */
4825678Spl196000 if (usr_fib_size == (sizeof (struct aac_srb) + \
4835678Spl196000 srb_sgcount * sizeof (struct aac_sg_entry64) - \
4845678Spl196000 sizeof (struct aac_sg_entry))) {
4855678Spl196000 sg64 = 1;
4865678Spl196000 } else if (usr_fib_size == (sizeof (struct aac_srb) + \
4875678Spl196000 (srb_sgcount - 1) * sizeof (struct aac_sg_entry))) {
4885678Spl196000 sg64 = 0;
4895678Spl196000 } else {
4905678Spl196000 rval = EINVAL;
4915678Spl196000 goto finish;
4925678Spl196000 }
4935678Spl196000
4945678Spl196000 /* Read user SG table */
4955678Spl196000 if ((usgt = kmem_zalloc(sizeof (struct aac_umem_sge) * srb_sgcount,
4965678Spl196000 KM_NOSLEEP)) == NULL) {
4975678Spl196000 rval = ENOMEM;
4985678Spl196000 goto finish;
4995678Spl196000 }
5005678Spl196000 for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
5015678Spl196000 if (sg64) {
5026799Sjd218194 struct aac_sg_entry64 *sg64p =
5036799Sjd218194 (struct aac_sg_entry64 *)srb->sg.SgEntry;
5046096Sjd218194
5056799Sjd218194 usge->bcount = sg64p->SgByteCount;
5066096Sjd218194 usge->addr = (caddr_t)
5076096Sjd218194 #ifndef _LP64
5086096Sjd218194 (uint32_t)
5096096Sjd218194 #endif
5106799Sjd218194 sg64p->SgAddress;
5115678Spl196000 } else {
5126799Sjd218194 struct aac_sg_entry *sgp = srb->sg.SgEntry;
5136799Sjd218194
5146799Sjd218194 usge->bcount = sgp->SgByteCount;
5155678Spl196000 usge->addr = (caddr_t)
5165678Spl196000 #ifdef _LP64
5175678Spl196000 (uint64_t)
5185678Spl196000 #endif
5196799Sjd218194 sgp->SgAddress;
5205678Spl196000 }
5215678Spl196000 acp->bcount += usge->bcount;
5225678Spl196000 if (usge->addr < addrlo)
5235678Spl196000 addrlo = usge->addr;
5245678Spl196000 if ((usge->addr + usge->bcount) > addrhi)
5255678Spl196000 addrhi = usge->addr + usge->bcount;
5265678Spl196000 }
5275678Spl196000 if (acp->bcount > softs->buf_dma_attr.dma_attr_maxxfer) {
5285678Spl196000 AACDB_PRINT(softs, CE_NOTE,
5295678Spl196000 "large srb xfer size received %d\n", acp->bcount);
5305678Spl196000 rval = EINVAL;
5315678Spl196000 goto finish;
5325678Spl196000 }
5335678Spl196000
5345678Spl196000 /* Lock user buffers */
5355678Spl196000 if (srb->flags & SRB_DataIn) {
5365678Spl196000 umem_flags |= DDI_UMEMLOCK_READ;
5375678Spl196000 direct |= B_READ;
5385678Spl196000 }
5395678Spl196000 if (srb->flags & SRB_DataOut) {
5405678Spl196000 umem_flags |= DDI_UMEMLOCK_WRITE;
5415678Spl196000 direct |= B_WRITE;
5425678Spl196000 }
5435678Spl196000 addrlo = (caddr_t)((uintptr_t)addrlo & (uintptr_t)PAGEMASK);
5445678Spl196000 rval = ddi_umem_lock(addrlo, (((size_t)addrhi + PAGEOFFSET) & \
5455678Spl196000 PAGEMASK) - (size_t)addrlo, umem_flags, &cookie);
5465678Spl196000 if (rval != 0) {
5475678Spl196000 AACDB_PRINT(softs, CE_NOTE, "ddi_umem_lock failed: %d",
5485678Spl196000 rval);
5495678Spl196000 goto finish;
5505678Spl196000 }
5515678Spl196000 locked = 1;
5525678Spl196000
5535678Spl196000 /* Allocate DMA for user buffers */
5545678Spl196000 for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
5555678Spl196000 struct buf *bp;
5565678Spl196000
5577100Spl196000 bp = ddi_umem_iosetup(cookie, (uintptr_t)usge->addr - \
5587100Spl196000 (uintptr_t)addrlo, usge->bcount, direct, dev, 0, NULL,
5597567SXin.Chen@Sun.COM DDI_UMEM_NOSLEEP);
5605678Spl196000 if (bp == NULL) {
5615678Spl196000 AACDB_PRINT(softs, CE_NOTE, "ddi_umem_iosetup failed");
5627567SXin.Chen@Sun.COM rval = ENOMEM;
5635678Spl196000 goto finish;
5645678Spl196000 }
5655678Spl196000 if (aac_cmd_dma_alloc(softs, &usge->acp, bp, 0, NULL_FUNC,
5665678Spl196000 0) != AACOK) {
5675678Spl196000 rval = EFAULT;
5685678Spl196000 goto finish;
5695678Spl196000 }
5705678Spl196000 acp->left_cookien += usge->acp.left_cookien;
5715678Spl196000 if (acp->left_cookien > softs->aac_sg_tablesize) {
5725678Spl196000 AACDB_PRINT(softs, CE_NOTE, "large cookiec received %d",
5735678Spl196000 acp->left_cookien);
5745678Spl196000 rval = EINVAL;
5755678Spl196000 goto finish;
5765678Spl196000 }
5775678Spl196000 }
5785678Spl196000
5795678Spl196000 /* Construct aac cmd SG table */
5805678Spl196000 if ((sge = kmem_zalloc(sizeof (struct aac_sge) * acp->left_cookien,
5815678Spl196000 KM_NOSLEEP)) == NULL) {
5825678Spl196000 rval = ENOMEM;
5835678Spl196000 goto finish;
5845678Spl196000 }
5855678Spl196000 acp->sgt = sge;
5865678Spl196000 for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
5875678Spl196000 for (sge0 = usge->acp.sgt;
5885678Spl196000 sge0 < &usge->acp.sgt[usge->acp.left_cookien];
5895678Spl196000 sge0++, sge++)
5905678Spl196000 *sge = *sge0;
5915678Spl196000 }
5925678Spl196000
5935678Spl196000 send_fib:
5945678Spl196000 acp->cmdlen = srb->cdb_size;
5955678Spl196000 acp->timeout = srb->timeout;
5965678Spl196000
5975678Spl196000 /* Send FIB command */
5985678Spl196000 acp->aac_cmd_fib = softs->aac_cmd_fib_scsi;
5997567SXin.Chen@Sun.COM #ifdef DEBUG
6007567SXin.Chen@Sun.COM acp->fib_flags = AACDB_FLAGS_FIB_SRB;
6017567SXin.Chen@Sun.COM #endif
6025678Spl196000 if ((rval = aac_send_fib(softs, acp)) != 0)
6035678Spl196000 goto finish;
6045678Spl196000
6055678Spl196000 /* Status struct */
6065678Spl196000 if (ddi_copyout((struct aac_srb_reply *)fibp->data,
6075678Spl196000 ((uint8_t *)arg + usr_fib_size),
6085678Spl196000 sizeof (struct aac_srb_reply), mode) != 0) {
6095678Spl196000 rval = EFAULT;
6105678Spl196000 goto finish;
6115678Spl196000 }
6125678Spl196000
6135678Spl196000 rval = 0;
6145678Spl196000 finish:
6155678Spl196000 if (acp->sgt)
6165678Spl196000 kmem_free(acp->sgt, sizeof (struct aac_sge) * \
6175678Spl196000 acp->left_cookien);
6185678Spl196000 if (usgt) {
6195678Spl196000 for (usge = usgt; usge < &usgt[srb_sgcount]; usge++) {
6205678Spl196000 if (usge->acp.sgt)
6215678Spl196000 kmem_free(usge->acp.sgt,
6225678Spl196000 sizeof (struct aac_sge) * \
6235678Spl196000 usge->acp.left_cookien);
6245678Spl196000 aac_free_dmamap(&usge->acp);
6255678Spl196000 if (usge->acp.bp)
6265678Spl196000 freerbuf(usge->acp.bp);
6275678Spl196000 }
6285678Spl196000 kmem_free(usgt, sizeof (struct aac_umem_sge) * srb_sgcount);
6295678Spl196000 }
6305678Spl196000 if (locked)
6315678Spl196000 ddi_umem_unlock(cookie);
6325678Spl196000 kmem_free(acp, sizeof (struct aac_cmd) + usr_fib_size + \
6335678Spl196000 sizeof (struct aac_fib_header));
6345678Spl196000 return (rval);
6355678Spl196000 }
6365678Spl196000
6375678Spl196000 /*ARGSUSED*/
6385678Spl196000 static int
aac_get_pci_info(struct aac_softstate * softs,intptr_t arg,int mode)6395678Spl196000 aac_get_pci_info(struct aac_softstate *softs, intptr_t arg, int mode)
6405678Spl196000 {
6415678Spl196000 union aac_pci_info_align un;
6425678Spl196000 struct aac_pci_info *resp = &un.d;
64311348SZhongyan.Gu@Sun.COM pci_regspec_t *pci_rp;
64411348SZhongyan.Gu@Sun.COM uint_t num;
6455678Spl196000
6465678Spl196000 DBCALLED(softs, 2);
6475678Spl196000
64811348SZhongyan.Gu@Sun.COM if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, softs->devinfo_p,
64911348SZhongyan.Gu@Sun.COM DDI_PROP_DONTPASS, "reg", (int **)&pci_rp, &num) !=
65011348SZhongyan.Gu@Sun.COM DDI_PROP_SUCCESS)
65111348SZhongyan.Gu@Sun.COM return (EINVAL);
65211348SZhongyan.Gu@Sun.COM if (num < (sizeof (pci_regspec_t) / sizeof (int))) {
65311348SZhongyan.Gu@Sun.COM ddi_prop_free(pci_rp);
65411348SZhongyan.Gu@Sun.COM return (EINVAL);
65511348SZhongyan.Gu@Sun.COM }
65611348SZhongyan.Gu@Sun.COM
65711348SZhongyan.Gu@Sun.COM resp->bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
65811348SZhongyan.Gu@Sun.COM resp->slot = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
65911348SZhongyan.Gu@Sun.COM ddi_prop_free(pci_rp);
6605678Spl196000
6615678Spl196000 if (ddi_copyout(resp, (void *)arg,
6625678Spl196000 sizeof (struct aac_pci_info), mode) != 0)
6635678Spl196000 return (EFAULT);
6645678Spl196000 return (0);
6655678Spl196000 }
6665678Spl196000
6675678Spl196000 static int
aac_query_disk(struct aac_softstate * softs,intptr_t arg,int mode)6685678Spl196000 aac_query_disk(struct aac_softstate *softs, intptr_t arg, int mode)
6695678Spl196000 {
6705678Spl196000 union aac_query_disk_align un;
6715678Spl196000 struct aac_query_disk *qdisk = &un.d;
6725678Spl196000 struct aac_container *dvp;
6735678Spl196000
6745678Spl196000 DBCALLED(softs, 2);
6755678Spl196000
6765678Spl196000 if (ddi_copyin((void *)arg, qdisk, sizeof (*qdisk), mode) != 0)
6775678Spl196000 return (EFAULT);
6785678Spl196000
6795678Spl196000 if (qdisk->container_no == -1) {
6805678Spl196000 qdisk->container_no = qdisk->target * 16 + qdisk->lun;
6815678Spl196000 } else if (qdisk->bus == -1 && qdisk->target == -1 &&
6825678Spl196000 qdisk->lun == -1) {
6835678Spl196000 if (qdisk->container_no >= AAC_MAX_CONTAINERS)
6845678Spl196000 return (EINVAL);
6855678Spl196000 qdisk->bus = 0;
6865678Spl196000 qdisk->target = (qdisk->container_no & 0xf);
6875678Spl196000 qdisk->lun = (qdisk->container_no >> 4);
6885678Spl196000 } else {
6895678Spl196000 return (EINVAL);
6905678Spl196000 }
6915678Spl196000
6925678Spl196000 mutex_enter(&softs->io_lock);
6935678Spl196000 dvp = &softs->containers[qdisk->container_no];
6947567SXin.Chen@Sun.COM qdisk->valid = AAC_DEV_IS_VALID(&dvp->dev);
6955678Spl196000 qdisk->locked = dvp->locked;
6965678Spl196000 qdisk->deleted = dvp->deleted;
6975678Spl196000 mutex_exit(&softs->io_lock);
6985678Spl196000
6995678Spl196000 if (ddi_copyout(qdisk, (void *)arg, sizeof (*qdisk), mode) != 0)
7005678Spl196000 return (EFAULT);
7015678Spl196000 return (0);
7025678Spl196000 }
7035678Spl196000
7045678Spl196000 static int
aac_delete_disk(struct aac_softstate * softs,intptr_t arg,int mode)7055678Spl196000 aac_delete_disk(struct aac_softstate *softs, intptr_t arg, int mode)
7065678Spl196000 {
7075678Spl196000 union aac_delete_disk_align un;
7085678Spl196000 struct aac_delete_disk *ddisk = &un.d;
7095678Spl196000 struct aac_container *dvp;
7105678Spl196000 int rval = 0;
7115678Spl196000
7125678Spl196000 DBCALLED(softs, 2);
7135678Spl196000
7145678Spl196000 if (ddi_copyin((void *)arg, ddisk, sizeof (*ddisk), mode) != 0)
7155678Spl196000 return (EFAULT);
7165678Spl196000
7175678Spl196000 if (ddisk->container_no >= AAC_MAX_CONTAINERS)
7185678Spl196000 return (EINVAL);
7195678Spl196000
7205678Spl196000 mutex_enter(&softs->io_lock);
7215678Spl196000 dvp = &softs->containers[ddisk->container_no];
7225678Spl196000 /*
7235678Spl196000 * We don't trust the userland to tell us when to delete
7245678Spl196000 * a container, rather we rely on an AIF coming from the
7255678Spl196000 * controller.
7265678Spl196000 */
7277567SXin.Chen@Sun.COM if (AAC_DEV_IS_VALID(&dvp->dev)) {
7285678Spl196000 if (dvp->locked)
7295678Spl196000 rval = EBUSY;
7305678Spl196000 }
7315678Spl196000 mutex_exit(&softs->io_lock);
7325678Spl196000
7335678Spl196000 return (rval);
7345678Spl196000 }
7355678Spl196000
7365678Spl196000 /*
7375678Spl196000 * The following function comes from Adaptec to support creation of arrays
7385678Spl196000 * bigger than 2TB.
7395678Spl196000 */
7405678Spl196000 static int
aac_supported_features(struct aac_softstate * softs,intptr_t arg,int mode)7415678Spl196000 aac_supported_features(struct aac_softstate *softs, intptr_t arg, int mode)
7425678Spl196000 {
7435678Spl196000 union aac_features_align un;
7445678Spl196000 struct aac_features *f = &un.d;
7455678Spl196000
7465678Spl196000 DBCALLED(softs, 2);
7475678Spl196000
7485678Spl196000 if (ddi_copyin((void *)arg, f, sizeof (*f), mode) != 0)
7495678Spl196000 return (EFAULT);
7505678Spl196000
7515678Spl196000 /*
7525678Spl196000 * When the management driver receives FSACTL_GET_FEATURES ioctl with
7535678Spl196000 * ALL zero in the featuresState, the driver will return the current
7545678Spl196000 * state of all the supported features, the data field will not be
7555678Spl196000 * valid.
7565678Spl196000 * When the management driver receives FSACTL_GET_FEATURES ioctl with
7575678Spl196000 * a specific bit set in the featuresState, the driver will return the
7585678Spl196000 * current state of this specific feature and whatever data that are
7595678Spl196000 * associated with the feature in the data field or perform whatever
7605678Spl196000 * action needed indicates in the data field.
7615678Spl196000 */
7625678Spl196000 if (f->feat.fValue == 0) {
7635678Spl196000 f->feat.fBits.largeLBA =
7645678Spl196000 (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
765*12408SZhongyan.Gu@Sun.COM f->feat.fBits.JBODSupport =
766*12408SZhongyan.Gu@Sun.COM (softs->flags & AAC_FLAGS_JBOD) ? 1 : 0;
7675678Spl196000 /* TODO: In the future, add other features state here as well */
7685678Spl196000 } else {
7695678Spl196000 if (f->feat.fBits.largeLBA)
7705678Spl196000 f->feat.fBits.largeLBA =
7715678Spl196000 (softs->flags & AAC_FLAGS_LBA_64BIT) ? 1 : 0;
772*12408SZhongyan.Gu@Sun.COM if (f->feat.fBits.JBODSupport)
773*12408SZhongyan.Gu@Sun.COM f->feat.fBits.JBODSupport =
774*12408SZhongyan.Gu@Sun.COM (softs->flags & AAC_FLAGS_JBOD) ? 1 : 0;
7755678Spl196000 /* TODO: Add other features state and data in the future */
7765678Spl196000 }
7775678Spl196000
7785678Spl196000 if (ddi_copyout(f, (void *)arg, sizeof (*f), mode) != 0)
7795678Spl196000 return (EFAULT);
7805678Spl196000 return (0);
7815678Spl196000 }
782