summaryrefslogtreecommitdiff
path: root/usr/src/lib/storage/libg_fc/common/cmd.c
blob: 53602345a4fc64c34b71a1b1f2c860126b9317ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*LINTLIBRARY*/

/*
 *  This module is part of the photon Command Line
 *  Interface program.
 *
 */

/*
 * I18N message number ranges
 *  This file: 9500 - 9999
 *  Shared common messages: 1 - 1999
 */

/*	Includes	*/
#include	<stdlib.h>
#include	<stdio.h>
#include	<sys/types.h>
#include	<unistd.h>
#include	<errno.h>
#include	<string.h>
#include	<sys/scsi/scsi.h>
#include	<nl_types.h>
#include	<sys/time.h>
#include	<l_common.h>
#include	<stgcom.h>
#include	<l_error.h>
#include	<g_state.h>


/*	Defines		*/
#define	MAXLEN		1000


/*	Global variables	*/
extern	nl_catd l_catd;


/*	External functions	*/
extern	int	rand_r(unsigned int *);


static int
wait_random_time(void)
{
time_t		timeval;
struct tm	*tmbuf = NULL;
struct timeval	tval;
unsigned int	seed;
int		random;
pid_t		pid;


	/*
	 * Get the system time and use "system seconds"
	 * as 'seed' to generate a random number. Then,
	 * wait between 1/10 - 1/2 seconds before retry.
	 * Get the current process id and ex-or it with
	 * the seed so that the random number is always
	 * different even in case of multiple processes
	 * generate a random number at the same time.
	 */
	if ((timeval = time(NULL)) == -1) {
		return (errno);
	}
	if ((tmbuf = localtime(&timeval)) == NULL) {
		return (L_LOCALTIME_ERROR);
	}

	pid = getpid();

	/* get a random number. */
	seed = (unsigned int) tmbuf->tm_sec;
	seed ^= pid;
	random = rand_r(&seed);


	random = ((random % 500) + 100) * MILLISEC;
	tval.tv_sec = random / MICROSEC;
	tval.tv_usec = random % MICROSEC;

	if (select(0, NULL, NULL, NULL, &tval) == -1) {
		return (L_SELECT_ERROR);
	}
	return (0);
}



/*
 * Execute a command and determine the result.
 */
int
cmd(int file, struct uscsi_cmd *command, int flag)
{
struct scsi_extended_sense	*rqbuf;
int				status, i, retry_cnt = 0, err;
char				errorMsg[MAXLEN];

	/*
	 * Set function flags for driver.
	 *
	 * Set Automatic request sense enable
	 *
	 */
	command->uscsi_flags = USCSI_RQENABLE;
	command->uscsi_flags |= flag;

	/* intialize error message array */
	errorMsg[0] = '\0';

	/* print command for debug */
	if (getenv("_LUX_S_DEBUG") != NULL) {
		if ((command->uscsi_cdb == NULL) ||
			(flag & USCSI_RESET) ||
			(flag & USCSI_RESET_ALL)) {
			if (flag & USCSI_RESET) {
				(void) printf("  Issuing a SCSI Reset.\n");
			}
			if (flag & USCSI_RESET_ALL) {
				(void) printf("  Issuing a SCSI Reset All.\n");
			}

		} else {
			(void) printf("  Issuing the following "
				"SCSI command: %s\n",
			g_scsi_find_command_name(command->uscsi_cdb[0]));
			(void) printf("	fd=0x%x cdb=", file);
			for (i = 0; i < (int)command->uscsi_cdblen; i++) {
				(void) printf("%x ", *(command->uscsi_cdb + i));
			}
			(void) printf("\n\tlen=0x%x bufaddr=0x%x buflen=0x%x"
				" flags=0x%x\n",
			command->uscsi_cdblen,
			command->uscsi_bufaddr,
			command->uscsi_buflen, command->uscsi_flags);

			if ((command->uscsi_buflen > 0) &&
				((flag & USCSI_READ) == 0)) {
				(void) g_dump("  Buffer data: ",
				(uchar_t *)command->uscsi_bufaddr,
				MIN(command->uscsi_buflen, 512), HEX_ASCII);
			}
		}
		fflush(stdout);
	}


	/*
	 * Default command timeout in case command left it 0
	 */
	if (command->uscsi_timeout == 0) {
		command->uscsi_timeout = 60;
	}
	/*	Issue command - finally */

retry:
	status = ioctl(file, USCSICMD, command);
	if (status == 0 && command->uscsi_status == 0) {
		if (getenv("_LUX_S_DEBUG") != NULL) {
			if ((command->uscsi_buflen > 0) &&
				(flag & USCSI_READ)) {
				(void) g_dump("\tData read:",
				(uchar_t *)command->uscsi_bufaddr,
				MIN(command->uscsi_buflen, 512), HEX_ASCII);
			}
		}
		return (status);
	}
	if ((status != 0) && (command->uscsi_status == 0)) {
		if ((getenv("_LUX_S_DEBUG") != NULL) ||
			(getenv("_LUX_ER_DEBUG") != NULL)) {
			(void) printf("Unexpected USCSICMD ioctl error: %s\n",
				strerror(errno));
		}
		return (status);
	}

	/*
	 * Just a SCSI error, create error message
	 * Retry once for Unit Attention,
	 * Not Ready, and Aborted Command
	 */
	if ((command->uscsi_rqbuf != NULL) &&
	    (((char)command->uscsi_rqlen - (char)command->uscsi_rqresid) > 0)) {

		rqbuf = (struct scsi_extended_sense *)command->uscsi_rqbuf;

		switch (rqbuf->es_key) {
		case KEY_NOT_READY:
			if (retry_cnt++ < 1) {
				ER_DPRINTF("Note: Device Not Ready."
						" Retrying...\n");

				if ((err = wait_random_time()) == 0) {
					goto retry;
				} else {
					return (err);
				}
			}
			break;

		case KEY_UNIT_ATTENTION:
			if (retry_cnt++ < 1) {
				ER_DPRINTF("  cmd():"
				" UNIT_ATTENTION: Retrying...\n");

				goto retry;
			}
			break;

		case KEY_ABORTED_COMMAND:
			if (retry_cnt++ < 1) {
				ER_DPRINTF("Note: Command is aborted."
				" Retrying...\n");

				goto retry;
			}
			break;
		}
		if ((getenv("_LUX_S_DEBUG") != NULL) ||
			(getenv("_LUX_ER_DEBUG") != NULL)) {
			g_scsi_printerr(command,
			(struct scsi_extended_sense *)command->uscsi_rqbuf,
			(command->uscsi_rqlen - command->uscsi_rqresid),
				errorMsg, strerror(errno));
		}

	} else {

		/*
		 * Retry 5 times in case of BUSY, and only
		 * once for Reservation-conflict, Command
		 * Termination and Queue Full. Wait for
		 * random amount of time (between 1/10 - 1/2 secs.)
		 * between each retry. This random wait is to avoid
		 * the multiple threads being executed at the same time
		 * and also the constraint in Photon IB, where the
		 * command queue has a depth of one command.
		 */
		switch ((uchar_t)command->uscsi_status & STATUS_MASK) {
		case STATUS_BUSY:
			if (retry_cnt++ < 5) {
				if ((err = wait_random_time()) == 0) {
					R_DPRINTF("  cmd(): No. of retries %d."
					" STATUS_BUSY: Retrying...\n",
					retry_cnt);
					goto retry;

				} else {
					return (err);
				}
			}
			break;

		case STATUS_RESERVATION_CONFLICT:
			if (retry_cnt++ < 1) {
				if ((err = wait_random_time()) == 0) {
					R_DPRINTF("  cmd():"
					" RESERVATION_CONFLICT:"
					" Retrying...\n");
					goto retry;

				} else {
					return (err);
				}
			}
			break;

		case STATUS_TERMINATED:
			if (retry_cnt++ < 1) {
				R_DPRINTF("Note: Command Terminated."
					" Retrying...\n");

				if ((err = wait_random_time()) == 0) {
					goto retry;
				} else {
					return (err);
				}
			}
			break;

		case STATUS_QFULL:
			if (retry_cnt++ < 1) {
				R_DPRINTF("Note: Command Queue is full."
				" Retrying...\n");

				if ((err = wait_random_time()) == 0) {
					goto retry;
				} else {
					return (err);
				}
			}
			break;
		}

	}
	if (((getenv("_LUX_S_DEBUG") != NULL) ||
		(getenv("_LUX_ER_DEBUG") != NULL)) &&
		(errorMsg[0] != '\0')) {
		(void) fprintf(stdout, "  %s\n", errorMsg);
	}
	return (L_SCSI_ERROR | command->uscsi_status);
}