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		1000 /* 1/100 second */
36#define	    BUSY_RETRY_TIMER	5000000000ULL /* Retry for 5 seconds */
37#ifdef	__cplusplus
38extern "C" {
39#endif
40
41/**
42 * @memo	    Send a read capacity 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	    fcLun Indicates the target unit to send command to
50 * @param	    responseBuffer User-allocated response buffer
51 * @param	    responseSize Size of User-allocated response buffer
52 * @param	    scsiStatus User-allocated scsi status byte
53 *
54 * @doc		    This routine will attempt a limited number of retries
55 *		    When busy or again errors are encountered.
56 */
57HBA_STATUS
58Sun_fcScsiReadCapacityV2(HBA_HANDLE handle, HBA_WWN portWWN,
59	    HBA_WWN targetPortWWN, HBA_UINT64 fcLun,
60	    void *responseBuffer, HBA_UINT32 *responseSize,
61	    HBA_UINT8 *scsiStatus,
62	    void *senseBuffer, HBA_UINT32 *senseSize) {
63	Trace log("Sun_fcScsiReadCapacityV2");
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->sendReadCapacity(wwnConversion(targetPortWWN.wwn),
73			fcLun, responseBuffer, responseSize, scsiStatus,
74			senseBuffer, senseSize);
75		return (HBA_STATUS_OK);
76	    } catch (BusyException &e) {
77		usleep(BUSY_SLEEP);
78		continue;
79	    } catch (TryAgainException &e) {
80		usleep(BUSY_SLEEP);
81		continue;
82	    } catch (HBAException &e) {
83		return (e.getErrorCode());
84	    } catch (...) {
85		log.internalError(
86		    "Uncaught exception");
87		return (HBA_STATUS_ERROR);
88	    }
89	}
90	return (HBA_STATUS_ERROR_TRY_AGAIN);
91}
92#ifdef	__cplusplus
93}
94#endif
95