1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26/*
27 * Copyright 2019 Joyent, Inc.
28 */
29#include <sun_sas.h>
30
31/*
32 * Returns the number of HBAs supported by the library.  This returns the
33 * current number of HBAs, even if this changes
34 *
35 */
36HBA_UINT32
37Sun_sasGetPortType(HBA_HANDLE handle, HBA_UINT32 port, HBA_PORTTYPE *porttype)
38{
39	const char		    ROUTINE[] = "Sun_sasGetPortType";
40	int			    index;
41	struct  sun_sas_hba	    *hba_ptr;
42	struct  sun_sas_port	    *hba_port_ptr;
43
44	/* Validate the arguments */
45	if (porttype == NULL) {
46		log(LOG_DEBUG, ROUTINE, "NULL attributes.");
47		return (HBA_STATUS_ERROR_ARG);
48	}
49
50	lock(&all_hbas_lock);
51	index = RetrieveIndex(handle);
52	lock(&open_handles_lock);
53	hba_ptr = RetrieveHandle(index);
54	if (hba_ptr == NULL) {
55		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx.", handle);
56		/* on error, need to set NumberOfEntries to 0 */
57		unlock(&open_handles_lock);
58		unlock(&all_hbas_lock);
59		return (HBA_STATUS_ERROR_INVALID_HANDLE);
60	}
61
62	if (hba_ptr->first_port == NULL) {
63		/* This is probably an internal failure of the library */
64		if (hba_ptr->device_path[0] != '\0') {
65			log(LOG_DEBUG, ROUTINE,
66			    "Internal failure:  Adapter %s contains no port "
67			    "data.", hba_ptr->device_path);
68		} else {
69			log(LOG_DEBUG, ROUTINE,
70			    "Internal failure:  Adapter at index %d contains "
71			    "no port data", hba_ptr->index);
72		}
73		unlock(&open_handles_lock);
74		unlock(&all_hbas_lock);
75		return (HBA_STATUS_ERROR);
76	}
77
78	for (hba_port_ptr = hba_ptr->first_port;
79	    hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next) {
80		if (hba_port_ptr->index == port) {
81			break;
82		}
83	}
84
85	if (hba_port_ptr == NULL || hba_port_ptr->index != port) {
86		log(LOG_DEBUG, ROUTINE,
87		    "Invalid port index %d for handle %08lx.",
88		    port, handle);
89		unlock(&open_handles_lock);
90		unlock(&all_hbas_lock);
91		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
92	}
93
94	*porttype = HBA_PORTTYPE_SASDEVICE;
95
96	unlock(&open_handles_lock);
97	unlock(&all_hbas_lock);
98
99	return (HBA_STATUS_OK);
100}
101