xref: /titanic_52/usr/src/lib/sun_fc/common/Sun_fcAdapterCreateWWN.cc (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte 
27*fcf3ce44SJohn Forte 
28*fcf3ce44SJohn Forte #include <fstream>
29*fcf3ce44SJohn Forte #include "fcntl.h"
30*fcf3ce44SJohn Forte #include "Handle.h"
31*fcf3ce44SJohn Forte #include "Trace.h"
32*fcf3ce44SJohn Forte #include "Exceptions.h"
33*fcf3ce44SJohn Forte #include "sun_fc.h"
34*fcf3ce44SJohn Forte 
35*fcf3ce44SJohn Forte 
36*fcf3ce44SJohn Forte #ifdef  __cplusplus
37*fcf3ce44SJohn Forte extern "C" {
38*fcf3ce44SJohn Forte #endif
39*fcf3ce44SJohn Forte 
40*fcf3ce44SJohn Forte void get_random_bytes(HBA_UINT8 *ptr, size_t len) {
41*fcf3ce44SJohn Forte 	int fd = open("/dev/urandom", O_RDONLY);
42*fcf3ce44SJohn Forte 	size_t resid = len;
43*fcf3ce44SJohn Forte 	ssize_t bytes;
44*fcf3ce44SJohn Forte 
45*fcf3ce44SJohn Forte 	while (resid != 0) {
46*fcf3ce44SJohn Forte 		bytes = read(fd, ptr, resid);
47*fcf3ce44SJohn Forte 		ptr += bytes;
48*fcf3ce44SJohn Forte 		resid -= bytes;
49*fcf3ce44SJohn Forte 	}
50*fcf3ce44SJohn Forte 	close (fd);
51*fcf3ce44SJohn Forte 	return;
52*fcf3ce44SJohn Forte }
53*fcf3ce44SJohn Forte 
54*fcf3ce44SJohn Forte HBA_STATUS Sun_fcAdapterCreateWWN(HBA_HANDLE handle,
55*fcf3ce44SJohn Forte     HBA_UINT32 portindex, HBA_WWN *nwwn, HBA_WWN *pwwn,
56*fcf3ce44SJohn Forte     HBA_WWN *OUI, HBA_INT32 method) {
57*fcf3ce44SJohn Forte 	HBA_UINT8	randombyte[5] = {0};
58*fcf3ce44SJohn Forte 	HBA_WWN		randomwwn = {0};
59*fcf3ce44SJohn Forte 	int		index = 0;
60*fcf3ce44SJohn Forte 
61*fcf3ce44SJohn Forte         Trace log("Sun_fcAdapterCreateWWN");
62*fcf3ce44SJohn Forte 
63*fcf3ce44SJohn Forte         if ((nwwn == NULL) || (pwwn == NULL)) {
64*fcf3ce44SJohn Forte                 log.userError(
65*fcf3ce44SJohn Forte                     "NULL WWN pointer");
66*fcf3ce44SJohn Forte                 return (HBA_STATUS_ERROR_ARG);
67*fcf3ce44SJohn Forte         }
68*fcf3ce44SJohn Forte 	if (method == HBA_CREATE_WWN_FACTORY) {
69*fcf3ce44SJohn Forte 		return (HBA_STATUS_ERROR_NOT_SUPPORTED);
70*fcf3ce44SJohn Forte 	}
71*fcf3ce44SJohn Forte 
72*fcf3ce44SJohn Forte         try {
73*fcf3ce44SJohn Forte 		/* create EUI-64 Mapped WWN */
74*fcf3ce44SJohn Forte 		if (OUI == NULL) {
75*fcf3ce44SJohn Forte 			/* if no OUI spec'd, used one of Sun's */
76*fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x0;
77*fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x0;
78*fcf3ce44SJohn Forte 			randomwwn.wwn[index++] = 0x7D;
79*fcf3ce44SJohn Forte 		} else {
80*fcf3ce44SJohn Forte 			memcpy(randomwwn.wwn, OUI->wwn, sizeof(HBA_WWN));
81*fcf3ce44SJohn Forte 			index += 3;
82*fcf3ce44SJohn Forte 		}
83*fcf3ce44SJohn Forte 		/*
84*fcf3ce44SJohn Forte 		 * for EUI-64 mapped, shift OUI first byte right two bits
85*fcf3ce44SJohn Forte 		 * then set top two bits to 11
86*fcf3ce44SJohn Forte 		 */
87*fcf3ce44SJohn Forte 		randomwwn.wwn[0] = randomwwn.wwn[0] >> 2;
88*fcf3ce44SJohn Forte 		randomwwn.wwn[0] = randomwwn.wwn[0] | 0xc0;
89*fcf3ce44SJohn Forte 
90*fcf3ce44SJohn Forte 		/* now create and add 40 random bits */
91*fcf3ce44SJohn Forte 		get_random_bytes(randombyte, 5);
92*fcf3ce44SJohn Forte 		memcpy(randomwwn.wwn+index, randombyte, 5);
93*fcf3ce44SJohn Forte 
94*fcf3ce44SJohn Forte 		memcpy(nwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
95*fcf3ce44SJohn Forte 
96*fcf3ce44SJohn Forte 		/* toggle lowest bit, to make NWWN and PWWN unique */
97*fcf3ce44SJohn Forte 		randomwwn.wwn[7] = randomwwn.wwn[7] ^ 1;
98*fcf3ce44SJohn Forte 		memcpy(pwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
99*fcf3ce44SJohn Forte 
100*fcf3ce44SJohn Forte                 return (HBA_STATUS_OK);
101*fcf3ce44SJohn Forte         } catch (HBAException &e) {
102*fcf3ce44SJohn Forte                 return (e.getErrorCode());
103*fcf3ce44SJohn Forte         } catch (...) {
104*fcf3ce44SJohn Forte                 log.internalError(
105*fcf3ce44SJohn Forte                     "Uncaught exception");
106*fcf3ce44SJohn Forte                 return (HBA_STATUS_ERROR);
107*fcf3ce44SJohn Forte         }
108*fcf3ce44SJohn Forte }
109*fcf3ce44SJohn Forte #ifdef  __cplusplus
110*fcf3ce44SJohn Forte }
111*fcf3ce44SJohn Forte #endif
112