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