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 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26
27
28#include "Trace.h"
29#include "Handle.h"
30#include "HBA.h"
31#include "HBAPort.h"
32#include "Exceptions.h"
33#include "sun_fc.h"
34#include <unistd.h>
35
36#define	    BUSY_SLEEP		1000000 /* 1/100 second */
37#define	    BUSY_RETRY_TIMER	5000000000ULL /* Retry for 5 seconds */
38#ifdef	__cplusplus
39extern "C" {
40#endif
41
42/**
43 * @memo	    Send a SCSI inquiry to a remote WWN
44 * @return	    HBA_STATUS_OK or other error code
45 *		    scsiStatus should be checked to ensure SCSI command
46 *		    was a success.
47 * @param	    handle The HBA to operate on
48 * @param	    portWWN Indicates the HBA port to send command through
49 * @param	    targetPortWWN Indicates the target to send command to
50 * @param	    fcLun Indicates the target unit to send command to
51 * @param	    cdb1 The first CDB byte
52 * @param	    cdb2 The second CDB byte
53 * @param	    responseBuffer User-allocated response buffer
54 * @param	    responseSize Size of User-allocated response buffer
55 * @param	    scsiStatus User-allocated scsi status byte
56 *
57 * @doc		    This routine will attempt a limited number of retries
58 *		    When busy or again errors are encountered.
59 */
60HBA_STATUS
61Sun_fcScsiInquiryV2(HBA_HANDLE handle, HBA_WWN portWWN, HBA_WWN targetPortWWN,
62	    HBA_UINT64 fcLun, HBA_UINT8 cdb1, HBA_UINT8 cdb2,
63	    void *responseBuffer, HBA_UINT32 *responseSize,
64	    HBA_UINT8 *scsiStatus,
65	    void *senseBuffer, HBA_UINT32 *senseSize) {
66	Trace log("Sun_fcScsiInquiryV2");
67
68	hrtime_t start = gethrtime();
69	hrtime_t end = start + BUSY_RETRY_TIMER;
70	for (hrtime_t cur = start; cur < end; cur = gethrtime()) {
71	    try {
72		Handle *myHandle = Handle::findHandle(handle);
73		HBA *hba = myHandle->getHBA();
74		HBAPort *port = hba->getPort(wwnConversion(portWWN.wwn));
75
76		port->sendScsiInquiry(wwnConversion(targetPortWWN.wwn), fcLun,
77			cdb1, cdb2, responseBuffer, responseSize,
78			scsiStatus, senseBuffer, senseSize);
79		return (HBA_STATUS_OK);
80	    } catch (BusyException &e) {
81		usleep(BUSY_SLEEP);
82		continue;
83	    } catch (TryAgainException &e) {
84		usleep(BUSY_SLEEP);
85		continue;
86	    } catch (HBAException &e) {
87		return (e.getErrorCode());
88	    } catch (...) {
89		log.internalError(
90		    "Uncaught exception");
91		return (HBA_STATUS_ERROR);
92	    }
93	}
94	return (HBA_STATUS_ERROR_TRY_AGAIN);
95}
96#ifdef	__cplusplus
97}
98#endif
99