2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A
2N/A#include <fstream>
2N/A#include <unistd.h>
2N/A#include "fcntl.h"
2N/A#include "Handle.h"
2N/A#include "Trace.h"
2N/A#include "Exceptions.h"
2N/A#include "sun_fc.h"
2N/A
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/Avoid get_random_bytes(HBA_UINT8 *ptr, size_t len) {
2N/A int fd = open("/dev/urandom", O_RDONLY);
2N/A size_t resid = len;
2N/A ssize_t bytes;
2N/A
2N/A while (resid != 0) {
2N/A bytes = read(fd, ptr, resid);
2N/A ptr += bytes;
2N/A resid -= bytes;
2N/A }
2N/A close (fd);
2N/A return;
2N/A}
2N/A
2N/AHBA_STATUS Sun_fcAdapterCreateWWN(HBA_HANDLE handle,
2N/A HBA_UINT32 portindex, HBA_WWN *nwwn, HBA_WWN *pwwn,
2N/A HBA_WWN *OUI, HBA_INT32 method) {
2N/A HBA_UINT8 randombyte[5] = {0};
2N/A HBA_WWN randomwwn = {0};
2N/A int index = 0;
2N/A
2N/A Trace log("Sun_fcAdapterCreateWWN");
2N/A
2N/A if ((nwwn == NULL) || (pwwn == NULL)) {
2N/A log.userError(
2N/A "NULL WWN pointer");
2N/A return (HBA_STATUS_ERROR_ARG);
2N/A }
2N/A if (method == HBA_CREATE_WWN_FACTORY) {
2N/A return (HBA_STATUS_ERROR_NOT_SUPPORTED);
2N/A }
2N/A
2N/A try {
2N/A /* create EUI-64 Mapped WWN */
2N/A if (OUI == NULL) {
2N/A /* if no OUI spec'd, used one of Sun's */
2N/A randomwwn.wwn[index++] = 0x0;
2N/A randomwwn.wwn[index++] = 0x0;
2N/A randomwwn.wwn[index++] = 0x7D;
2N/A } else {
2N/A memcpy(randomwwn.wwn, OUI->wwn, sizeof(HBA_WWN));
2N/A index += 3;
2N/A }
2N/A /*
2N/A * for EUI-64 mapped, shift OUI first byte right two bits
2N/A * then set top two bits to 11
2N/A */
2N/A randomwwn.wwn[0] = randomwwn.wwn[0] >> 2;
2N/A randomwwn.wwn[0] = randomwwn.wwn[0] | 0xc0;
2N/A
2N/A /* now create and add 40 random bits */
2N/A get_random_bytes(randombyte, 5);
2N/A memcpy(randomwwn.wwn+index, randombyte, 5);
2N/A
2N/A memcpy(nwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
2N/A
2N/A /* toggle lowest bit, to make NWWN and PWWN unique */
2N/A randomwwn.wwn[7] = randomwwn.wwn[7] ^ 1;
2N/A memcpy(pwwn->wwn, randomwwn.wwn, sizeof(HBA_WWN));
2N/A
2N/A return (HBA_STATUS_OK);
2N/A } catch (HBAException &e) {
2N/A return (e.getErrorCode());
2N/A } catch (...) {
2N/A log.internalError(
2N/A "Uncaught exception");
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A}
2N/A#ifdef __cplusplus
2N/A}
2N/A#endif