19e86db79SHyon Kim /*
29e86db79SHyon Kim  * CDDL HEADER START
39e86db79SHyon Kim  *
49e86db79SHyon Kim  * The contents of this file are subject to the terms of the
59e86db79SHyon Kim  * Common Development and Distribution License (the "License").
69e86db79SHyon Kim  * You may not use this file except in compliance with the License.
79e86db79SHyon Kim  *
89e86db79SHyon Kim  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99e86db79SHyon Kim  * or http://www.opensolaris.org/os/licensing.
109e86db79SHyon Kim  * See the License for the specific language governing permissions
119e86db79SHyon Kim  * and limitations under the License.
129e86db79SHyon Kim  *
139e86db79SHyon Kim  * When distributing Covered Code, include this CDDL HEADER in each
149e86db79SHyon Kim  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159e86db79SHyon Kim  * If applicable, add the following below this CDDL HEADER, with the
169e86db79SHyon Kim  * fields enclosed by brackets "[]" replaced with your own identifying
179e86db79SHyon Kim  * information: Portions Copyright [yyyy] [name of copyright owner]
189e86db79SHyon Kim  *
199e86db79SHyon Kim  * CDDL HEADER END
209e86db79SHyon Kim  */
219e86db79SHyon Kim 
229e86db79SHyon Kim /*
239e86db79SHyon Kim  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
249e86db79SHyon Kim  * Use is subject to license terms.
259e86db79SHyon Kim  */
269e86db79SHyon Kim 
279e86db79SHyon Kim 
289e86db79SHyon Kim #ifndef	_SUN_SAS_H
299e86db79SHyon Kim #define	_SUN_SAS_H
309e86db79SHyon Kim 
319e86db79SHyon Kim #ifdef	__cplusplus
329e86db79SHyon Kim extern "C" {
339e86db79SHyon Kim #endif
349e86db79SHyon Kim 
359e86db79SHyon Kim #include <smhbaapi.h>
369e86db79SHyon Kim #include <vendorsmhbaapi.h>
379e86db79SHyon Kim #include <sys/types.h>
389e86db79SHyon Kim #include <sys/stat.h>
399e86db79SHyon Kim #include <sys/param.h>
409e86db79SHyon Kim #include <dirent.h>
419e86db79SHyon Kim #include <fcntl.h>
429e86db79SHyon Kim #include <stdio.h>
439e86db79SHyon Kim #include <stdlib.h>
449e86db79SHyon Kim #include <strings.h>
459e86db79SHyon Kim #include <string.h>
469e86db79SHyon Kim #include <limits.h>
479e86db79SHyon Kim #include <syslog.h>
489e86db79SHyon Kim #include <thread.h>
499e86db79SHyon Kim #include <synch.h>
509e86db79SHyon Kim #include <unistd.h>
519e86db79SHyon Kim #include <stropts.h>
529e86db79SHyon Kim #include <libdevinfo.h>
539e86db79SHyon Kim #include <sys/time.h>
549e86db79SHyon Kim #include <sys/scsi/generic/commands.h>
559e86db79SHyon Kim #include <sys/scsi/impl/commands.h>
569e86db79SHyon Kim #include <sys/scsi/impl/sense.h>
579e86db79SHyon Kim #include <sys/scsi/generic/inquiry.h>
589e86db79SHyon Kim #include <sys/scsi/impl/uscsi.h>
599e86db79SHyon Kim #include <sys/varargs.h>
609e86db79SHyon Kim #include <sys/varargs.h>
619e86db79SHyon Kim #include <libsysevent.h>
629e86db79SHyon Kim 
639e86db79SHyon Kim #ifdef	__cplusplus
649e86db79SHyon Kim extern "C" {
659e86db79SHyon Kim #endif
669e86db79SHyon Kim 
679e86db79SHyon Kim #define	VSL_NUMERIC_VERSION	1
689e86db79SHyon Kim #define	VSL_STRING_VERSION	"Version 1"
699e86db79SHyon Kim #define	VSL_NAME		"Sun T11 SM-HBA Vendor Library for SAS HBAs"
719e86db79SHyon Kim 
729e86db79SHyon Kim /* The /dev links we expose */
739e86db79SHyon Kim #define	DEV_DISK_DIR		"/dev/rdsk"
749e86db79SHyon Kim #define	DEV_TAPE_DIR		"/dev/rmt"
759e86db79SHyon Kim #define	DEV_ES_DIR		"/dev/es"
769e86db79SHyon Kim #define	DEV_CFG_DIR		"/dev/cfg"
779e86db79SHyon Kim #define	DEVICES_DIR		"/devices"
789e86db79SHyon Kim #define	DEVCTL_SUFFIX		":devctl"
799e86db79SHyon Kim #define	SCSI_SUFFIX		":scsi"
809e86db79SHyon Kim 
819e86db79SHyon Kim /* To be consistent, when out of memory call this macro routine */
829e86db79SHyon Kim #define	OUT_OF_MEMORY(routine)  \
839e86db79SHyon Kim     log(LOG_DEBUG, routine, "Out of memory.")
849e86db79SHyon Kim 
859e86db79SHyon Kim #define	S_FREE(x)   (((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
869e86db79SHyon Kim 
879e86db79SHyon Kim #define	IS_STUB_NODE(s) (di_instance(s) == -1 && \
889e86db79SHyon Kim 	di_nodeid(s) == (DI_PROM_NODEID))
899e86db79SHyon Kim 
909e86db79SHyon Kim /* manf+model+10(char length of UINTMAX)+6(for 2 -'s, NULL & extra 3bytes */
919e86db79SHyon Kim #define	HANDLE_NAME_LENGTH  (64 + 256 + 10 + 6)
929e86db79SHyon Kim #define	HANDLE_ERROR	0 /* This is an error condition */
939e86db79SHyon Kim 
949e86db79SHyon Kim /* Some timing values */
959e86db79SHyon Kim #define	LOCK_SLEEP	    1
969e86db79SHyon Kim #define	BUSY_SLEEP	    10000 /* 1/100 second */
979e86db79SHyon Kim #define	BUSY_RETRY_TIMER    5000000000 /* Retry for 5 seconds */
989e86db79SHyon Kim #define	STATE_RETRY_TIMER   10000000000 /* Retry for 10 seconds */
999e86db79SHyon Kim #define	HR_SECOND	    1000000000
1009e86db79SHyon Kim /* How many times to silently retry, before starting to print warnings */
1019e86db79SHyon Kim #define	DEADLOCK_WARNING    10
1029e86db79SHyon Kim 
1039e86db79SHyon Kim #define	MAX_LUN		4096
1049e86db79SHyon Kim #define	REP_LUNS_RSP_SIZE   sizeof (rep_luns_rsp_t)+  \
1059e86db79SHyon Kim 				(sizeof (lun_list_element_t)*(MAX_LUN - 1))
1069e86db79SHyon Kim 
1079e86db79SHyon Kim /* misc */
1089e86db79SHyon Kim #define	SUN_MICROSYSTEMS	"Sun Microsystems, Inc."
1099e86db79SHyon Kim 
110*c1ba8596SToomas Soome extern mutex_t		all_hbas_lock;
111*c1ba8596SToomas Soome extern mutex_t		open_handles_lock;
112*c1ba8596SToomas Soome extern mutex_t		log_file_lock;
113*c1ba8596SToomas Soome extern HBA_UINT32	hba_count;
114*c1ba8596SToomas Soome extern HBA_UINT16	open_handle_index;
1159e86db79SHyon Kim 
1169e86db79SHyon Kim 
1179e86db79SHyon Kim /* Internal structures that aren't exposed to clients */
1189e86db79SHyon Kim struct open_handle {
1199e86db79SHyon Kim 	int			adapterIndex;
1209e86db79SHyon Kim 	HBA_UINT32		handle;
1219e86db79SHyon Kim 	struct open_handle	*next;
1229e86db79SHyon Kim };
1239e86db79SHyon Kim 
1249e86db79SHyon Kim struct sun_sas_hba {
1259e86db79SHyon Kim 	HBA_UINT32		index;  /* Can be sparse */
1269e86db79SHyon Kim 	struct open_handle	*open_handles;
1279e86db79SHyon Kim 	int			fd;	    /* when open, the FD */
1289e86db79SHyon Kim 	/* The libdevinfo HBA path (lacking /devices) */
1299e86db79SHyon Kim 	char			device_path[MAXPATHLEN];
1309e86db79SHyon Kim 	char			handle_name[HANDLE_NAME_LENGTH];
1319e86db79SHyon Kim 	SMHBA_ADAPTERATTRIBUTES	adapter_attributes;
1329e86db79SHyon Kim 
1339e86db79SHyon Kim 	/* State tracking */
1349e86db79SHyon Kim 	boolean_t		invalid;
1359e86db79SHyon Kim 	struct sun_sas_hba	*next;
1369e86db79SHyon Kim 	struct sun_sas_port	*first_port;
1379e86db79SHyon Kim };
1389e86db79SHyon Kim 
139*c1ba8596SToomas Soome extern struct sun_sas_hba *global_hba_head;
1409e86db79SHyon Kim 
1419e86db79SHyon Kim struct ScsiEntryList {
1429e86db79SHyon Kim 	SMHBA_SCSIENTRY		entry;
1439e86db79SHyon Kim 	struct ScsiEntryList	*next;
1449e86db79SHyon Kim };
1459e86db79SHyon Kim 
1469e86db79SHyon Kim struct phy_info {
1479e86db79SHyon Kim 	HBA_UINT32		index;
1489e86db79SHyon Kim 	boolean_t		invalid;
1499e86db79SHyon Kim 	SMHBA_SAS_PHY		phy;
1509e86db79SHyon Kim 	struct phy_info		*next;
1519e86db79SHyon Kim };
1529e86db79SHyon Kim 
1539e86db79SHyon Kim struct sun_sas_port {
1549e86db79SHyon Kim 	HBA_UINT32		index;
1559e86db79SHyon Kim 	boolean_t		invalid;
1569e86db79SHyon Kim 
1579e86db79SHyon Kim 	/* The libdevinfo HBA path (lacking /devices) */
1589e86db79SHyon Kim 	char			device_path[MAXPATHLEN];
1599e86db79SHyon Kim 	SMHBA_PORTATTRIBUTES	port_attributes;
1609e86db79SHyon Kim 	struct ScsiEntryList	*scsiInfo;
1619e86db79SHyon Kim 	int			cntlNumber;
1629e86db79SHyon Kim 
1639e86db79SHyon Kim 	/* The following are used to track the device map */
1649e86db79SHyon Kim 	int			num_devices;
1659e86db79SHyon Kim 	struct sun_sas_port	*first_attached_port; /* Only for HBA port */
1669e86db79SHyon Kim 	struct phy_info		*first_phy;	/* Only for HBA port */
1679e86db79SHyon Kim 	struct sun_sas_port	*next;
1689e86db79SHyon Kim };
1699e86db79SHyon Kim 
1709e86db79SHyon Kim typedef struct walkarg {
1719e86db79SHyon Kim 	char *devpath;
1729e86db79SHyon Kim 	boolean_t *flag;
1739e86db79SHyon Kim } walkarg_t;
1749e86db79SHyon Kim 
1759e86db79SHyon Kim extern int	loadCount;
1769e86db79SHyon Kim 
1779e86db79SHyon Kim extern sysevent_handle_t *gSysEventHandle;
1789e86db79SHyon Kim 
1799e86db79SHyon Kim /* External routines */
1809e86db79SHyon Kim extern HBA_STATUS SMHBA_RegisterLibrary(PSMHBA_ENTRYPOINTS);
1819e86db79SHyon Kim extern HBA_UINT32 Sun_sasGetVendorLibraryAttributes(SMHBA_LIBRARYATTRIBUTES *);
1829e86db79SHyon Kim extern HBA_STATUS Sun_sasGetAdapterAttributes(HBA_HANDLE,
1839e86db79SHyon Kim     SMHBA_ADAPTERATTRIBUTES *);
1849e86db79SHyon Kim extern HBA_UINT32 Sun_sasGetNumberOfAdapters();
1859e86db79SHyon Kim extern HBA_STATUS Sun_sasGetAdapterName(HBA_UINT32, char *);
1869e86db79SHyon Kim extern HBA_STATUS Sun_sasGetPortType(HBA_HANDLE, HBA_UINT32, HBA_PORTTYPE *);
1879e86db79SHyon Kim extern HBA_STATUS Sun_sasGetAdapterPortAttributes(HBA_HANDLE, HBA_UINT32,
1889e86db79SHyon Kim     SMHBA_PORTATTRIBUTES *);
1899e86db79SHyon Kim extern HBA_STATUS Sun_sasGetPortAttributesByWWN(HBA_HANDLE, HBA_WWN, HBA_WWN,
1909e86db79SHyon Kim     SMHBA_PORTATTRIBUTES *);
1919e86db79SHyon Kim extern HBA_STATUS Sun_sasGetFCPhyAttributes(HBA_HANDLE, HBA_UINT32, HBA_UINT32,
1929e86db79SHyon Kim     SMHBA_FC_PHY *);
1939e86db79SHyon Kim extern HBA_STATUS Sun_sasGetSASPhyAttributes(HBA_HANDLE, HBA_UINT32,
1949e86db79SHyon Kim     HBA_UINT32, SMHBA_SAS_PHY *);
1959e86db79SHyon Kim extern HBA_STATUS Sun_sasGetProtocolStatistics(HBA_HANDLE, HBA_UINT32,
1979e86db79SHyon Kim extern HBA_STATUS Sun_sasGetPhyStatistics(HBA_HANDLE, HBA_UINT32,
1989e86db79SHyon Kim     HBA_UINT32, SMHBA_PHYSTATISTICS *);
1999e86db79SHyon Kim extern HBA_STATUS Sun_sasSendSMPPassThru(HBA_HANDLE, HBA_WWN,  HBA_WWN, HBA_WWN,
2009e86db79SHyon Kim     void *, HBA_UINT32, void *, HBA_UINT32 *);
2019e86db79SHyon Kim extern HBA_STATUS Sun_sasGetBindingCapability(HBA_HANDLE, HBA_WWN, HBA_WWN,
2029e86db79SHyon Kim     SMHBA_BIND_CAPABILITY *);
2039e86db79SHyon Kim extern HBA_STATUS Sun_sasGetBindingSupport(HBA_HANDLE, HBA_WWN, HBA_WWN,
2049e86db79SHyon Kim     SMHBA_BIND_CAPABILITY *);
2059e86db79SHyon Kim extern HBA_STATUS Sun_sasSetBindingSupport(HBA_HANDLE, HBA_WWN, HBA_WWN,
2069e86db79SHyon Kim     SMHBA_BIND_CAPABILITY);
2079e86db79SHyon Kim extern HBA_STATUS Sun_sasGetTargetMapping(HBA_HANDLE, HBA_WWN, HBA_WWN,
2089e86db79SHyon Kim     SMHBA_TARGETMAPPING *);
2099e86db79SHyon Kim extern HBA_STATUS Sun_sasGetPersistentBinding(HBA_HANDLE, HBA_WWN, HBA_WWN,
2109e86db79SHyon Kim     SMHBA_BINDING *);
2119e86db79SHyon Kim extern HBA_STATUS Sun_sasSetPersistentBinding(HBA_HANDLE, HBA_WWN, HBA_WWN,
2129e86db79SHyon Kim     const SMHBA_BINDING *);
2139e86db79SHyon Kim extern HBA_STATUS Sun_sasRemovePersistentBinding(HBA_HANDLE, HBA_WWN, HBA_WWN,
2149e86db79SHyon Kim     const SMHBA_BINDING *);
2159e86db79SHyon Kim extern HBA_STATUS Sun_sasRemoveAllPersistentBindings(HBA_HANDLE, HBA_WWN,
2169e86db79SHyon Kim     HBA_WWN);
2179e86db79SHyon Kim extern HBA_STATUS Sun_sasGetLUNStatistics(HBA_HANDLE, const HBA_SCSIID *,
2189e86db79SHyon Kim     SMHBA_PROTOCOLSTATISTICS *);
2199e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForAdapterAddEvents(void (*)(void *, HBA_WWN,
2209e86db79SHyon Kim     HBA_UINT32), void *, HBA_CALLBACKHANDLE *);
2219e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForAdapterEvents(void (*)(void *, HBA_WWN,
2229e86db79SHyon Kim     HBA_UINT32), void *, HBA_HANDLE, HBA_CALLBACKHANDLE *);
2239e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForAdapterPortEvents(void (*)(void *, HBA_WWN,
2249e86db79SHyon Kim     HBA_UINT32, HBA_UINT32), void *, HBA_HANDLE, HBA_WWN, HBA_UINT32,
2259e86db79SHyon Kim     HBA_CALLBACKHANDLE *);
2269e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForAdapterPortStatEvents(void (*)(void *,
2279e86db79SHyon Kim     HBA_WWN, HBA_UINT32, HBA_UINT32), void *, HBA_HANDLE, HBA_WWN, HBA_UINT32,
2299e86db79SHyon Kim extern HBA_STATUS    Sun_sasRegisterForAdapterPhyStatEvents(void (*)(void *,
2309e86db79SHyon Kim     HBA_WWN, HBA_UINT32, HBA_UINT32), void *, HBA_HANDLE, HBA_WWN, HBA_UINT32,
2329e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForTargetEvents(void (*)(void *, HBA_WWN,
2339e86db79SHyon Kim     HBA_WWN, HBA_WWN, HBA_UINT32), void *, HBA_HANDLE, HBA_WWN, HBA_WWN,
2349e86db79SHyon Kim     HBA_WWN, HBA_CALLBACKHANDLE *, HBA_UINT32);
2359e86db79SHyon Kim extern HBA_STATUS Sun_sasRegisterForLinkEvents(void (*)(void *, HBA_WWN,
2369e86db79SHyon Kim     HBA_UINT32, void *, HBA_UINT32), void *, void *, HBA_UINT32, HBA_HANDLE,
2379e86db79SHyon Kim     HBA_CALLBACKHANDLE *);
2389e86db79SHyon Kim extern HBA_STATUS Sun_sasScsiInquiry(HBA_HANDLE, HBA_WWN, HBA_WWN, HBA_WWN,
2399e86db79SHyon Kim     SMHBA_SCSILUN, HBA_UINT8, HBA_UINT8, void *, HBA_UINT32 *, HBA_UINT8 *,
2409e86db79SHyon Kim     void *, HBA_UINT32 *);
2419e86db79SHyon Kim extern HBA_STATUS Sun_sasScsiReportLUNs(HBA_HANDLE, HBA_WWN, HBA_WWN,
2429e86db79SHyon Kim     HBA_WWN, void *, HBA_UINT32 *, HBA_UINT8 *, void *, HBA_UINT32 *);
2439e86db79SHyon Kim extern HBA_STATUS Sun_sasScsiReadCapacity(HBA_HANDLE, HBA_WWN, HBA_WWN, HBA_WWN,
2449e86db79SHyon Kim     SMHBA_SCSILUN, void *, HBA_UINT32 *, HBA_UINT8 *, void *, HBA_UINT32 *);
2459e86db79SHyon Kim extern HBA_UINT32 Sun_sasGetVersion();
2469e86db79SHyon Kim extern HBA_STATUS Sun_sasLoadLibrary();
2479e86db79SHyon Kim extern HBA_STATUS Sun_sasFreeLibrary();
2489e86db79SHyon Kim extern HBA_UINT32 Sun_sasGetNumberOfAdapters();
2499e86db79SHyon Kim extern HBA_UINT32 Sun_sasGetNumberOfPorts(HBA_HANDLE, HBA_UINT32 *);
2509e86db79SHyon Kim extern HBA_STATUS Sun_sasGetAdapterName(HBA_UINT32, char *);
2519e86db79SHyon Kim extern HBA_HANDLE Sun_sasOpenAdapter(char *);
2529e86db79SHyon Kim extern void Sun_sasCloseAdapter(HBA_HANDLE);
2539e86db79SHyon Kim extern HBA_STATUS Sun_sasGetDiscoveredPortAttributes(HBA_HANDLE, HBA_UINT32,
2549e86db79SHyon Kim     HBA_UINT32, SMHBA_PORTATTRIBUTES *);
2559e86db79SHyon Kim extern HBA_STATUS Sun_sasGetPortAttributesByWWN(HBA_HANDLE, HBA_WWN, HBA_WWN,
2569e86db79SHyon Kim     SMHBA_PORTATTRIBUTES *);
2579e86db79SHyon Kim extern void Sun_sasRefreshInformation(HBA_HANDLE);
2589e86db79SHyon Kim extern void Sun_sasRefreshAdapterConfiguration(void);
2599e86db79SHyon Kim extern HBA_STATUS Sun_sasRemoveCallback(HBA_CALLBACKHANDLE);
2609e86db79SHyon Kim 
2619e86db79SHyon Kim 
2629e86db79SHyon Kim /* Internal routines */
2639e86db79SHyon Kim extern void log(int, const char *, char *, ...);
2649e86db79SHyon Kim extern u_longlong_t wwnConversion(uchar_t *wwn);
2659e86db79SHyon Kim extern HBA_STATUS devtree_attached_devices(di_node_t, struct sun_sas_port *);
2669e86db79SHyon Kim extern HBA_HANDLE CreateHandle(int);
2679e86db79SHyon Kim extern int RetrieveIndex(HBA_HANDLE);
2689e86db79SHyon Kim extern struct open_handle *RetrieveOpenHandle(HBA_HANDLE);
2699e86db79SHyon Kim extern struct sun_sas_hba *RetrieveHandle(int);
2709e86db79SHyon Kim extern struct sun_sas_hba *ExtractHandle(int);
2719e86db79SHyon Kim extern struct sun_sas_hba *Retrieve_Sun_sasHandle(HBA_HANDLE);
2729e86db79SHyon Kim extern void lock(mutex_t *mp);
2739e86db79SHyon Kim extern void unlock(mutex_t *mp);
2749e86db79SHyon Kim extern void reportSense(struct scsi_extended_sense *, const char *);
2759e86db79SHyon Kim extern HBA_STATUS verifyAdapter(struct sun_sas_hba *hba_ptr);
2769e86db79SHyon Kim extern HBA_STATUS devtree_get_all_hbas(di_node_t root);
2779e86db79SHyon Kim extern HBA_STATUS devtree_get_one_hba(di_node_t node);
2789e86db79SHyon Kim extern HBA_STATUS FreeHBA(struct sun_sas_hba *hba);
2799e86db79SHyon Kim extern HBA_WWN getFirstAdapterPortWWN(HBA_HANDLE handle);
2809e86db79SHyon Kim extern HBA_STATUS getPortStateCounter(char *fpPath, HBA_UINT32 *stateCount);
2819e86db79SHyon Kim extern HBA_STATUS lookupControllerLink(char *path, char *link);
2829e86db79SHyon Kim extern HBA_STATUS lookupSMPLink(char *path, char *link);
2839e86db79SHyon Kim extern void convertDevpathToDevlink(PSMHBA_TARGETMAPPING mappings);
2849e86db79SHyon Kim extern void fillDomainPortWWN(struct sun_sas_port *);
2859e86db79SHyon Kim extern HBA_STATUS get_phy_info(di_node_t, struct sun_sas_port *);
2869e86db79SHyon Kim extern HBA_STATUS send_uscsi_cmd(const char *devpath, struct uscsi_cmd *ucmd);
2879e86db79SHyon Kim extern HBA_STATUS registerSysevent();
2889e86db79SHyon Kim extern HBA_STATUS validateDomainAddress(struct sun_sas_port *, HBA_WWN);
2899e86db79SHyon Kim 
2909e86db79SHyon Kim #ifdef	__cplusplus
2919e86db79SHyon Kim }
2929e86db79SHyon Kim #endif
2939e86db79SHyon Kim 
2949e86db79SHyon Kim #endif /* _SUN_SAS_H */