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