xref: /titanic_50/usr/src/cmd/format/analyze.c (revision 65908c77dfc02644236ba18bffe67b5ed6f23135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c92a0838Smishra  * Common Development and Distribution License (the "License").
6c92a0838Smishra  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*65908c77Syu, larry liu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains routines to analyze the surface of a disk.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include "global.h"
307c478bd9Sstevel@tonic-gate #include "analyze.h"
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include "misc.h"
347c478bd9Sstevel@tonic-gate #include "defect.h"
357c478bd9Sstevel@tonic-gate #include "label.h"
367c478bd9Sstevel@tonic-gate #include "param.h"
373e1bd7a2Ssjelinek #include "checkdev.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * These global variables control the surface analysis process.  They
427c478bd9Sstevel@tonic-gate  * are set from a command in the defect menu.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate int	scan_entire = 1;		/* scan whole disk flag */
457c478bd9Sstevel@tonic-gate diskaddr_t	scan_lower = 0;			/* lower bound */
467c478bd9Sstevel@tonic-gate diskaddr_t	scan_upper = 0;			/* upper bound */
477c478bd9Sstevel@tonic-gate int	scan_correct = 1;		/* correct errors flag */
487c478bd9Sstevel@tonic-gate int	scan_stop = 0;			/* stop after error flag */
497c478bd9Sstevel@tonic-gate int	scan_loop = 0;			/* loop forever flag */
507c478bd9Sstevel@tonic-gate int	scan_passes = 2;		/* number of passes */
517c478bd9Sstevel@tonic-gate int	scan_random = 0;		/* random patterns flag */
52342440ecSPrasad Singamsetty uint_t	scan_size = 0;			/* sectors/scan operation */
537c478bd9Sstevel@tonic-gate int	scan_auto = 1;			/* scan after format flag */
547c478bd9Sstevel@tonic-gate int	scan_restore_defects = 1;	/* restore defect list after writing */
557c478bd9Sstevel@tonic-gate int	scan_restore_label = 1;		/* restore label after writing */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * These are summary variables to print out info after analysis.
597c478bd9Sstevel@tonic-gate  * Values less than 0 imply they are invalid.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate offset_t	scan_cur_block = -1;		/* current block */
627c478bd9Sstevel@tonic-gate int64_t		scan_blocks_fixed = -1;		/* # blocks repaired */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * This variable is used to tell whether the most recent surface
667c478bd9Sstevel@tonic-gate  * analysis error was caused by a media defect or some other problem.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate int	media_error;			/* error was caused by defect */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int	disk_error;			/* disk errors during analysis */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * These are the data patterns used if random patterns are not chosen.
747c478bd9Sstevel@tonic-gate  * They are designed to show pattern dependent errors.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate static unsigned int	scan_patterns[] = {
777c478bd9Sstevel@tonic-gate 	0xc6dec6de,
787c478bd9Sstevel@tonic-gate 	0x6db6db6d,
797c478bd9Sstevel@tonic-gate 	0x00000000,
807c478bd9Sstevel@tonic-gate 	0xffffffff,
817c478bd9Sstevel@tonic-gate 	0xaaaaaaaa,
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate #define	NPATTERNS	5		/* number of predefined patterns */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * These are the data patterns from the SunFed requirements document.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate static unsigned int purge_patterns[] = {	/* patterns to be written */
897c478bd9Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
907c478bd9Sstevel@tonic-gate 	0x55555555,		/* 01010101...  == UUUU... */
917c478bd9Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
927c478bd9Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static unsigned int alpha_pattern =  0x40404040;   /* 10000000...  == @@@@... */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* Function prototypes */
987c478bd9Sstevel@tonic-gate #ifdef	__STDC__
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int	scan_repair(diskaddr_t bn, int mode);
101342440ecSPrasad Singamsetty static int	analyze_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
1027c478bd9Sstevel@tonic-gate 		unsigned data, int init, int driver_flags, int *xfercntp);
1037c478bd9Sstevel@tonic-gate static int	handle_error_conditions(void);
104342440ecSPrasad Singamsetty static int	verify_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
1057c478bd9Sstevel@tonic-gate 		unsigned data, int driver_flags, int *xfercntp);
1067c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int	scan_repair();
1097c478bd9Sstevel@tonic-gate static int	analyze_blocks();
1107c478bd9Sstevel@tonic-gate static int	handle_error_conditions();
1117c478bd9Sstevel@tonic-gate static int	verify_blocks();
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * This routine performs a surface analysis based upon the global
1177c478bd9Sstevel@tonic-gate  * parameters.  It is called from several commands in the defect menu,
1187c478bd9Sstevel@tonic-gate  * and from the format command in the command menu (if post-format
1197c478bd9Sstevel@tonic-gate  * analysis is enable).
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate int
do_scan(flags,mode)1227c478bd9Sstevel@tonic-gate do_scan(flags, mode)
1237c478bd9Sstevel@tonic-gate 	int	flags, mode;
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	diskaddr_t	start, end, curnt;
126342440ecSPrasad Singamsetty 	int	pass, needinit, data;
127342440ecSPrasad Singamsetty 	uint_t	size;
1287c478bd9Sstevel@tonic-gate 	int	status, founderr, i, j;
1297c478bd9Sstevel@tonic-gate 	int	error = 0;
1307c478bd9Sstevel@tonic-gate 	int	pattern = 0;
1317c478bd9Sstevel@tonic-gate 	int	xfercnt;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Check to be sure we aren't correcting without a defect list
1357c478bd9Sstevel@tonic-gate 	 * if the controller can correct the defect.
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 	if (scan_correct && !EMBEDDED_SCSI && (cur_ops->op_repair != NULL) &&
1387c478bd9Sstevel@tonic-gate 			(cur_list.list == NULL)) {
1397c478bd9Sstevel@tonic-gate 		err_print("Current Defect List must be initialized ");
1407c478bd9Sstevel@tonic-gate 		err_print("to do automatic repair.\n");
1417c478bd9Sstevel@tonic-gate 		return (-1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * Define the bounds of the scan.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	if (scan_entire) {
1477c478bd9Sstevel@tonic-gate 		start = 0;
1487c478bd9Sstevel@tonic-gate 	    if (cur_label == L_TYPE_SOLARIS) {
1497c478bd9Sstevel@tonic-gate 		if (cur_ctype->ctype_flags & CF_SCSI)
1507c478bd9Sstevel@tonic-gate 			end = datasects() - 1;
1517c478bd9Sstevel@tonic-gate 		else
1527c478bd9Sstevel@tonic-gate 			end = physsects() - 1;
1537c478bd9Sstevel@tonic-gate 	    } else if (cur_label == L_TYPE_EFI) {
1547c478bd9Sstevel@tonic-gate 		end = cur_parts->etoc->efi_last_lba;
1557c478bd9Sstevel@tonic-gate 	    }
1567c478bd9Sstevel@tonic-gate 	} else {
1577c478bd9Sstevel@tonic-gate 		start = scan_lower;
1587c478bd9Sstevel@tonic-gate 		end = scan_upper;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Make sure the user knows if we are scanning over a mounted
1627c478bd9Sstevel@tonic-gate 	 * partition.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
1657c478bd9Sstevel@tonic-gate 	    (checkmount(start, end))) {
1667c478bd9Sstevel@tonic-gate 		err_print("Cannot do analysis on a mounted partition.\n");
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Make sure the user knows if we are scanning over a
1727c478bd9Sstevel@tonic-gate 	 * partition being used for swapping.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
1757c478bd9Sstevel@tonic-gate 	    (checkswap(start, end))) {
1767c478bd9Sstevel@tonic-gate 		err_print("Cannot do analysis on a partition \
1777c478bd9Sstevel@tonic-gate 		    which is currently being used for swapping.\n");
1787c478bd9Sstevel@tonic-gate 		return (-1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
182c92a0838Smishra 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
183c92a0838Smishra 	 * or live upgrade are on the disk.
184c92a0838Smishra 	 */
185c92a0838Smishra 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
186c92a0838Smishra 	    (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
187c92a0838Smishra 	    (diskaddr_t)-1, 0, 0))) {
188c92a0838Smishra 		err_print("Cannot do analysis on a partition "
189c92a0838Smishra 		    "while it in use as described above.\n");
190c92a0838Smishra 		return (-1);
191c92a0838Smishra 	}
192c92a0838Smishra 
193c92a0838Smishra 	/*
1947c478bd9Sstevel@tonic-gate 	 * If we are scanning destructively over certain sectors,
1957c478bd9Sstevel@tonic-gate 	 * we mark the defect list and/or label dirty so it will get rewritten.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	if (flags & (SCAN_PATTERN | SCAN_WRITE)) {
1987c478bd9Sstevel@tonic-gate 	    if (cur_label == L_TYPE_SOLARIS) {
199342440ecSPrasad Singamsetty 		if (start < (diskaddr_t)totalsects() &&
200342440ecSPrasad Singamsetty 				end >= (diskaddr_t)datasects()) {
2017c478bd9Sstevel@tonic-gate 			if (!EMBEDDED_SCSI) {
2027c478bd9Sstevel@tonic-gate 				cur_list.flags |= LIST_DIRTY;
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			if (cur_disk->disk_flags & DSK_LABEL)
2057c478bd9Sstevel@tonic-gate 				cur_flags |= LABEL_DIRTY;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	    }
2087c478bd9Sstevel@tonic-gate 	    if (start == 0) {
2097c478bd9Sstevel@tonic-gate 		if (cur_disk->disk_flags & DSK_LABEL)
2107c478bd9Sstevel@tonic-gate 			cur_flags |= LABEL_DIRTY;
2117c478bd9Sstevel@tonic-gate 	    }
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Initialize the summary info on sectors repaired.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	scan_blocks_fixed = 0;
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Loop through the passes of the scan. If required, loop forever.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	for (pass = 0; pass < scan_passes || scan_loop; pass++) {
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * Determine the data pattern to use if pattern testing
2237c478bd9Sstevel@tonic-gate 		 * is to be done.
2247c478bd9Sstevel@tonic-gate 		 */
2257c478bd9Sstevel@tonic-gate 		if (flags & SCAN_PATTERN) {
2267c478bd9Sstevel@tonic-gate 			if (scan_random)
2277c478bd9Sstevel@tonic-gate 				data = (int)mrand48();
2287c478bd9Sstevel@tonic-gate 			else
2297c478bd9Sstevel@tonic-gate 				data = scan_patterns[pass % NPPATTERNS];
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			if (flags & SCAN_PURGE) {
2327c478bd9Sstevel@tonic-gate 				flags &= ~(SCAN_PURGE_READ_PASS
2337c478bd9Sstevel@tonic-gate 						| SCAN_PURGE_ALPHA_PASS);
2347c478bd9Sstevel@tonic-gate 				switch (pattern % (NPPATTERNS + 1)) {
2357c478bd9Sstevel@tonic-gate 				case NPPATTERNS:
2367c478bd9Sstevel@tonic-gate 					pattern = 0;
2377c478bd9Sstevel@tonic-gate 					if (!error) {
2387c478bd9Sstevel@tonic-gate 					    fmt_print(
2397c478bd9Sstevel@tonic-gate "\nThe last %d passes were successful, running alpha pattern pass", NPPATTERNS);
2407c478bd9Sstevel@tonic-gate 					    flags |= SCAN_PURGE_ALPHA_PASS;
2417c478bd9Sstevel@tonic-gate 					    data = alpha_pattern;
2427c478bd9Sstevel@tonic-gate 					} else {
2437c478bd9Sstevel@tonic-gate 					    data = purge_patterns[pattern];
2447c478bd9Sstevel@tonic-gate 					    pattern++;
2457c478bd9Sstevel@tonic-gate 					};
2467c478bd9Sstevel@tonic-gate 					break;
2477c478bd9Sstevel@tonic-gate 				case READPATTERN:
2487c478bd9Sstevel@tonic-gate 					flags |=  SCAN_PURGE_READ_PASS;
2497c478bd9Sstevel@tonic-gate 				default:
2507c478bd9Sstevel@tonic-gate 					data = purge_patterns[pattern];
2517c478bd9Sstevel@tonic-gate 					pattern++;
2527c478bd9Sstevel@tonic-gate 					break;
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			fmt_print("\n        pass %d", pass);
2567c478bd9Sstevel@tonic-gate 			fmt_print(" - pattern = 0x%x", data);
2577c478bd9Sstevel@tonic-gate 		} else
2587c478bd9Sstevel@tonic-gate 			fmt_print("\n        pass %d", pass);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		fmt_print("\n");
2617c478bd9Sstevel@tonic-gate 		/*
2627c478bd9Sstevel@tonic-gate 		 * Mark the pattern buffer as corrupt, since it
2637c478bd9Sstevel@tonic-gate 		 * hasn't been initialized.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		needinit = 1;
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * Print the first block number to the log file if
2687c478bd9Sstevel@tonic-gate 		 * logging is on so there is some record of what
2697c478bd9Sstevel@tonic-gate 		 * analysis was performed.
2707c478bd9Sstevel@tonic-gate 		 */
2717c478bd9Sstevel@tonic-gate 		if (log_file) {
2727c478bd9Sstevel@tonic-gate 			pr_dblock(log_print, start);
2737c478bd9Sstevel@tonic-gate 			log_print("\n");
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 		/*
2767c478bd9Sstevel@tonic-gate 		 * Loop through this pass, each time analyzing an amount
2777c478bd9Sstevel@tonic-gate 		 * specified by the global parameters.
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		xfercnt = 0;
2807c478bd9Sstevel@tonic-gate 		for (curnt = start; curnt <= end; curnt += size) {
2817c478bd9Sstevel@tonic-gate 			if ((end - curnt) < scan_size)
2827c478bd9Sstevel@tonic-gate 				size = end - curnt + 1;
2837c478bd9Sstevel@tonic-gate 			else
2847c478bd9Sstevel@tonic-gate 				size = scan_size;
2857c478bd9Sstevel@tonic-gate 			/*
2867c478bd9Sstevel@tonic-gate 			 * Print out where we are, so we don't look dead.
2877c478bd9Sstevel@tonic-gate 			 * Also store it in summary info for logging.
2887c478bd9Sstevel@tonic-gate 			 */
2897c478bd9Sstevel@tonic-gate 			scan_cur_block = curnt;
2907c478bd9Sstevel@tonic-gate 			nolog_print("   ");
2917c478bd9Sstevel@tonic-gate 			pr_dblock(nolog_print, curnt);
2927c478bd9Sstevel@tonic-gate 			nolog_print("  \015");
2937c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
2947c478bd9Sstevel@tonic-gate 			disk_error = 0;
2957c478bd9Sstevel@tonic-gate 			/*
2967c478bd9Sstevel@tonic-gate 			 * Do the actual analysis.
2977c478bd9Sstevel@tonic-gate 			 */
2987f5d20e3Sny155746 			status = analyze_blocks(flags, curnt, size,
2997c478bd9Sstevel@tonic-gate 			    (unsigned)data, needinit, (F_ALLERRS | F_SILENT),
3007c478bd9Sstevel@tonic-gate 			    &xfercnt);
3017c478bd9Sstevel@tonic-gate 			/*
3027c478bd9Sstevel@tonic-gate 			 * If there were no errors, the pattern buffer is
3037c478bd9Sstevel@tonic-gate 			 * still initialized, and we just loop to next chunk.
3047c478bd9Sstevel@tonic-gate 			 */
3057c478bd9Sstevel@tonic-gate 			needinit = 0;
3067c478bd9Sstevel@tonic-gate 			if (!status)
3077c478bd9Sstevel@tonic-gate 				continue;
3087c478bd9Sstevel@tonic-gate 			/*
3097c478bd9Sstevel@tonic-gate 			 * There was an error. Check if surface analysis
3107c478bd9Sstevel@tonic-gate 			 * can be continued.
3117c478bd9Sstevel@tonic-gate 			 */
3127c478bd9Sstevel@tonic-gate 			if (handle_error_conditions()) {
3137c478bd9Sstevel@tonic-gate 				scan_blocks_fixed = scan_cur_block = -1;
3147c478bd9Sstevel@tonic-gate 				return (-1);
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 			/*
3177c478bd9Sstevel@tonic-gate 			 * There was an error. Mark the pattern buffer
3187c478bd9Sstevel@tonic-gate 			 * corrupt so it will get reinitialized.
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			needinit = 1;
3217c478bd9Sstevel@tonic-gate 			/*
3227c478bd9Sstevel@tonic-gate 			 * If it was not a media error, ignore it.
3237c478bd9Sstevel@tonic-gate 			 */
3247c478bd9Sstevel@tonic-gate 			if (!media_error)
3257c478bd9Sstevel@tonic-gate 				continue;
3267c478bd9Sstevel@tonic-gate 			/*
3277c478bd9Sstevel@tonic-gate 			 * Loop 5 times through each sector of the chunk,
3287c478bd9Sstevel@tonic-gate 			 * analyzing them individually.
3297c478bd9Sstevel@tonic-gate 			 */
3307c478bd9Sstevel@tonic-gate 			nolog_print("   ");
3317c478bd9Sstevel@tonic-gate 			pr_dblock(nolog_print, curnt);
3327c478bd9Sstevel@tonic-gate 			nolog_print("  \015");
3337c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
3347c478bd9Sstevel@tonic-gate 			founderr = 0;
3357c478bd9Sstevel@tonic-gate 			for (j = 0; j < size * 5; j++) {
3367c478bd9Sstevel@tonic-gate 				i = j % size;
3377c478bd9Sstevel@tonic-gate 				disk_error = 0;
3387f5d20e3Sny155746 				status = analyze_blocks(flags, (curnt + i), 1,
3397f5d20e3Sny155746 				    (unsigned)data, needinit, F_ALLERRS, NULL);
3407c478bd9Sstevel@tonic-gate 				needinit = 0;
3417c478bd9Sstevel@tonic-gate 				if (!status)
3427c478bd9Sstevel@tonic-gate 					continue;
3437c478bd9Sstevel@tonic-gate 				/*
3447c478bd9Sstevel@tonic-gate 				 * There was an error. Check if surface analysis
3457c478bd9Sstevel@tonic-gate 				 * can be continued.
3467c478bd9Sstevel@tonic-gate 				 */
3477c478bd9Sstevel@tonic-gate 				if (handle_error_conditions()) {
3487c478bd9Sstevel@tonic-gate 					scan_blocks_fixed = scan_cur_block = -1;
3497c478bd9Sstevel@tonic-gate 					return (-1);
3507c478bd9Sstevel@tonic-gate 				}
3517c478bd9Sstevel@tonic-gate 				/*
3527c478bd9Sstevel@tonic-gate 				 * An error occurred.  Mark the buffer
3537c478bd9Sstevel@tonic-gate 				 * corrupt and see if it was media
3547c478bd9Sstevel@tonic-gate 				 * related.
3557c478bd9Sstevel@tonic-gate 				 */
3567c478bd9Sstevel@tonic-gate 				needinit = 1;
3577c478bd9Sstevel@tonic-gate 				if (!media_error)
3587c478bd9Sstevel@tonic-gate 					continue;
3597c478bd9Sstevel@tonic-gate 				/*
3607c478bd9Sstevel@tonic-gate 				 * We found a bad sector. Print out a message
3617c478bd9Sstevel@tonic-gate 				 * and fix it if required.
3627c478bd9Sstevel@tonic-gate 				 */
3637c478bd9Sstevel@tonic-gate 				founderr = 1;
3647c478bd9Sstevel@tonic-gate 				if (scan_correct && (flags != SCAN_VALID)) {
3657c478bd9Sstevel@tonic-gate 					if (scan_repair(curnt+i, mode)) {
3667c478bd9Sstevel@tonic-gate 						error = -1;
3677c478bd9Sstevel@tonic-gate 					}
3687c478bd9Sstevel@tonic-gate 				} else
3697c478bd9Sstevel@tonic-gate 					err_print("\n");
3707c478bd9Sstevel@tonic-gate 				/*
3717c478bd9Sstevel@tonic-gate 				 * Stop after the error if required.
3727c478bd9Sstevel@tonic-gate 				 */
3737c478bd9Sstevel@tonic-gate 				if (scan_stop)
3747c478bd9Sstevel@tonic-gate 					goto out;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			/*
3777c478bd9Sstevel@tonic-gate 			 * Mark the pattern buffer corrupt to be safe.
3787c478bd9Sstevel@tonic-gate 			 */
3797c478bd9Sstevel@tonic-gate 			needinit = 1;
3807c478bd9Sstevel@tonic-gate 			/*
3817c478bd9Sstevel@tonic-gate 			 * We didn't find an individual sector that was bad.
3827c478bd9Sstevel@tonic-gate 			 * Print out a warning.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 			if (!founderr) {
3857c478bd9Sstevel@tonic-gate 				err_print("Warning: unable to pinpoint ");
3867c478bd9Sstevel@tonic-gate 				err_print("defective block.\n");
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * Print the end of each pass to the log file.
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 		enter_critical();
3937c478bd9Sstevel@tonic-gate 		if (log_file) {
3947c478bd9Sstevel@tonic-gate 			pr_dblock(log_print, scan_cur_block);
3957c478bd9Sstevel@tonic-gate 			log_print("\n");
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 		scan_cur_block = -1;
3987c478bd9Sstevel@tonic-gate 		exit_critical();
3997c478bd9Sstevel@tonic-gate 		fmt_print("\n");
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		/*
4027c478bd9Sstevel@tonic-gate 		 * alternate the read and write for SCAN_VERIFY test
4037c478bd9Sstevel@tonic-gate 		 */
4047c478bd9Sstevel@tonic-gate 		if (flags & SCAN_VERIFY) {
4057c478bd9Sstevel@tonic-gate 			flags ^= SCAN_VERIFY_READ_PASS;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate out:
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * We got here either by giving up after an error or falling
4117c478bd9Sstevel@tonic-gate 	 * through after all passes were completed.
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	fmt_print("\n");
4147c478bd9Sstevel@tonic-gate 	enter_critical();
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * If the defect list is dirty, write it to disk,
4177c478bd9Sstevel@tonic-gate 	 * if scan_restore_defects (the default) is true.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) &&
4207c478bd9Sstevel@tonic-gate 				(scan_restore_defects)) {
4217c478bd9Sstevel@tonic-gate 		cur_list.flags = 0;
4227c478bd9Sstevel@tonic-gate 		write_deflist(&cur_list);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * If the label is dirty, write it to disk.
4267c478bd9Sstevel@tonic-gate 	 * if scan_restore_label (the default) is true.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) {
4297c478bd9Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
4307c478bd9Sstevel@tonic-gate 		(void) write_label();
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * If we dropped down to here after an error, we need to write
4347c478bd9Sstevel@tonic-gate 	 * the final block number to the log file for record keeping.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if (log_file && scan_cur_block >= 0) {
4377c478bd9Sstevel@tonic-gate 		pr_dblock(log_print, scan_cur_block);
4387c478bd9Sstevel@tonic-gate 		log_print("\n");
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	fmt_print("Total of %lld defective blocks repaired.\n",
4417c478bd9Sstevel@tonic-gate 		scan_blocks_fixed);
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Reinitialize the logging variables so they don't get used
4447c478bd9Sstevel@tonic-gate 	 * when they are not really valid.
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	scan_blocks_fixed = scan_cur_block = -1;
4477c478bd9Sstevel@tonic-gate 	exit_critical();
4487c478bd9Sstevel@tonic-gate 	return (error);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * This routine is called to repair a bad block discovered
4547c478bd9Sstevel@tonic-gate  * during a scan operation.  Return 0 for success, 1 for failure.
4557c478bd9Sstevel@tonic-gate  * (This has been extracted out of do_scan(), to simplify it.)
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static int
scan_repair(bn,mode)4587c478bd9Sstevel@tonic-gate scan_repair(bn, mode)
4597c478bd9Sstevel@tonic-gate 	diskaddr_t	bn;
4607c478bd9Sstevel@tonic-gate 	int	mode;
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	int	status;
4637c478bd9Sstevel@tonic-gate 	int	result = 1;
464*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	char	*buf;
4657c478bd9Sstevel@tonic-gate 	int	buf_is_good;
4667c478bd9Sstevel@tonic-gate 	int	i;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (cur_ops->op_repair == NULL) {
4697c478bd9Sstevel@tonic-gate 		err_print("Warning: Controller does ");
4707c478bd9Sstevel@tonic-gate 		err_print("not support repairing.\n\n");
4717c478bd9Sstevel@tonic-gate 		return (result);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
474*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	buf = malloc(cur_blksz);
475*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (buf == NULL) {
476*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("Warning: no memory.\n\n");
477*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (result);
478*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
4797c478bd9Sstevel@tonic-gate 	enter_critical();
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Determine if the error appears to be hard or soft.  We
4837c478bd9Sstevel@tonic-gate 	 * already assume there's an error.  If we can get any
4847c478bd9Sstevel@tonic-gate 	 * good data out of the sector, write that data back
4857c478bd9Sstevel@tonic-gate 	 * after the repair.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	buf_is_good = 0;
4887c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
4897c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
4907c478bd9Sstevel@tonic-gate 				buf, F_SILENT, NULL);
4917c478bd9Sstevel@tonic-gate 		if (status == 0) {
4927c478bd9Sstevel@tonic-gate 			buf_is_good = 1;
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	fmt_print("Repairing %s error on %llu (",
4987c478bd9Sstevel@tonic-gate 				buf_is_good ? "soft" : "hard", bn);
4997c478bd9Sstevel@tonic-gate 	pr_dblock(fmt_print, bn);
5007c478bd9Sstevel@tonic-gate 	fmt_print(")...");
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	status = (*cur_ops->op_repair)(bn, mode);
5037c478bd9Sstevel@tonic-gate 	if (status) {
5047c478bd9Sstevel@tonic-gate 		/*
5057c478bd9Sstevel@tonic-gate 		 * If the repair failed, we note it and will return the
5067c478bd9Sstevel@tonic-gate 		 * failure. However, the analysis goes on.
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		fmt_print("failed.\n\n");
5097c478bd9Sstevel@tonic-gate 	} else {
5107c478bd9Sstevel@tonic-gate 		/*
5117c478bd9Sstevel@tonic-gate 		 * The repair worked.  Write the good data we could
5127c478bd9Sstevel@tonic-gate 		 * recover from the failed block, if possible.
5137c478bd9Sstevel@tonic-gate 		 * If not, zero the block.  In doing so, try to
5147c478bd9Sstevel@tonic-gate 		 * determine if the new block appears ok.
5157c478bd9Sstevel@tonic-gate 		 */
5167c478bd9Sstevel@tonic-gate 		if (!buf_is_good) {
517*65908c77Syu, larry liu - Sun Microsystems - Beijing China 			bzero(buf, cur_blksz);
5187c478bd9Sstevel@tonic-gate 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
5197c478bd9Sstevel@tonic-gate 		} else {
5207c478bd9Sstevel@tonic-gate 			fmt_print("ok.\n");
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
5237c478bd9Sstevel@tonic-gate 					1, buf, (F_SILENT | F_ALLERRS), NULL);
5247c478bd9Sstevel@tonic-gate 		if (status == 0) {
5257c478bd9Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
5267c478bd9Sstevel@tonic-gate 					1, buf, (F_SILENT | F_ALLERRS), NULL);
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		if (status) {
5297c478bd9Sstevel@tonic-gate 			fmt_print("The new block also appears defective.\n");
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		fmt_print("\n");
5327c478bd9Sstevel@tonic-gate 		/*
5337c478bd9Sstevel@tonic-gate 		 * add the defect to the list and write the list out.
5347c478bd9Sstevel@tonic-gate 		 * Also, kill the working list so it will get resynced
5357c478bd9Sstevel@tonic-gate 		 * with the current list.
5367c478bd9Sstevel@tonic-gate 		 *
5377c478bd9Sstevel@tonic-gate 		 * For embedded scsi, we don't require a defect list.
5387c478bd9Sstevel@tonic-gate 		 * However, if we have one, add the defect if the
5397c478bd9Sstevel@tonic-gate 		 * list includes the grown list.  If not, kill it
5407c478bd9Sstevel@tonic-gate 		 * to force a resync if we need the list later.
5417c478bd9Sstevel@tonic-gate 		 */
5427c478bd9Sstevel@tonic-gate 		if (EMBEDDED_SCSI) {
5437c478bd9Sstevel@tonic-gate 			if (cur_list.list != NULL) {
5447c478bd9Sstevel@tonic-gate 				if (cur_list.flags & LIST_PGLIST) {
5457c478bd9Sstevel@tonic-gate 					add_ldef(bn, &cur_list);
5467c478bd9Sstevel@tonic-gate 				} else {
5477c478bd9Sstevel@tonic-gate 					kill_deflist(&cur_list);
5487c478bd9Sstevel@tonic-gate 				}
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 		/*
5517c478bd9Sstevel@tonic-gate 		 * The next "if" statement reflects the fix for
5527c478bd9Sstevel@tonic-gate 		 * bug id 1026096 where format keeps adding the
5537c478bd9Sstevel@tonic-gate 		 * same defect to the defect list.
5547c478bd9Sstevel@tonic-gate 		 */
5557c478bd9Sstevel@tonic-gate 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
5567c478bd9Sstevel@tonic-gate 			kill_deflist(&cur_list);
5577c478bd9Sstevel@tonic-gate 			(*cur_ops->op_ex_cur)(&cur_list);
5587c478bd9Sstevel@tonic-gate 			fmt_print("Current list updated\n");
5597c478bd9Sstevel@tonic-gate 		} else {
5607c478bd9Sstevel@tonic-gate 			add_ldef(bn, &cur_list);
5617c478bd9Sstevel@tonic-gate 			write_deflist(&cur_list);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		kill_deflist(&work_list);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		/* Log the repair.  */
5667c478bd9Sstevel@tonic-gate 		scan_blocks_fixed++;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		/* return ok */
5697c478bd9Sstevel@tonic-gate 		result = 0;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	exit_critical();
573*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(buf);
5747c478bd9Sstevel@tonic-gate 	return (result);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * This routine analyzes a set of sectors on the disk.  It simply returns
5807c478bd9Sstevel@tonic-gate  * an error if a defect is found.  It is called by do_scan().
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate static int
analyze_blocks(flags,blkno,blkcnt,data,init,driver_flags,xfercntp)5837c478bd9Sstevel@tonic-gate analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
584342440ecSPrasad Singamsetty 	int	flags, driver_flags, init;
585342440ecSPrasad Singamsetty 	uint_t	blkcnt;
5867c478bd9Sstevel@tonic-gate 	register unsigned data;
5877c478bd9Sstevel@tonic-gate 	diskaddr_t	blkno;
5887c478bd9Sstevel@tonic-gate 	int	*xfercntp;
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	int		corrupt = 0;
591342440ecSPrasad Singamsetty 	int		status;
592342440ecSPrasad Singamsetty 	register diskaddr_t	i, nints;
5937c478bd9Sstevel@tonic-gate 	register unsigned *ptr = (uint_t *)pattern_buf;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	media_error = 0;
5967c478bd9Sstevel@tonic-gate 	if (flags & SCAN_VERIFY) {
5977c478bd9Sstevel@tonic-gate 		return (verify_blocks(flags, blkno, blkcnt, data,
5987c478bd9Sstevel@tonic-gate 		    driver_flags, xfercntp));
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * Initialize the pattern buffer if necessary.
6037c478bd9Sstevel@tonic-gate 	 */
604*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	nints = (diskaddr_t)blkcnt * cur_blksz / sizeof (int);
6057c478bd9Sstevel@tonic-gate 	if ((flags & SCAN_PATTERN) && init) {
6067c478bd9Sstevel@tonic-gate 		for (i = 0; i < nints; i++)
6077c478bd9Sstevel@tonic-gate 			*((int *)((int *)pattern_buf + i)) = data;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * Lock out interrupts so we can insure valid data will get
6117c478bd9Sstevel@tonic-gate 	 * restored. This is necessary because there are modes
6127c478bd9Sstevel@tonic-gate 	 * of scanning that corrupt the disk data then restore it at
6137c478bd9Sstevel@tonic-gate 	 * the end of the analysis.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	enter_critical();
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * If the disk data is valid, read it into the data buffer.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	if (flags & SCAN_VALID) {
6207c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
6217c478bd9Sstevel@tonic-gate 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
6227c478bd9Sstevel@tonic-gate 		if (status)
6237c478bd9Sstevel@tonic-gate 			goto bad;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * If we are doing pattern testing, write and read the pattern
6277c478bd9Sstevel@tonic-gate 	 * from the pattern buffer.
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	if (flags & SCAN_PATTERN) {
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * If the disk data was valid, mark it corrupt so we know
6327c478bd9Sstevel@tonic-gate 		 * to restore it later.
6337c478bd9Sstevel@tonic-gate 		 */
6347c478bd9Sstevel@tonic-gate 		if (flags & SCAN_VALID)
6357c478bd9Sstevel@tonic-gate 			corrupt++;
6367c478bd9Sstevel@tonic-gate 		/*
6377c478bd9Sstevel@tonic-gate 		 * Only write if we're not on the read pass of SCAN_PURGE.
6387c478bd9Sstevel@tonic-gate 		 */
6397c478bd9Sstevel@tonic-gate 		if (!(flags & SCAN_PURGE_READ_PASS)) {
6407c478bd9Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6417c478bd9Sstevel@tonic-gate 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
6427c478bd9Sstevel@tonic-gate 			    xfercntp);
6437c478bd9Sstevel@tonic-gate 			if (status)
6447c478bd9Sstevel@tonic-gate 			    goto bad;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		/*
6477c478bd9Sstevel@tonic-gate 		 * Only read if we are on the read pass of SCAN_PURGE, if we
6487c478bd9Sstevel@tonic-gate 		 * are purging.
6497c478bd9Sstevel@tonic-gate 		 */
6507c478bd9Sstevel@tonic-gate 		if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) {
6517c478bd9Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
6527c478bd9Sstevel@tonic-gate 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
6537c478bd9Sstevel@tonic-gate 			    xfercntp);
6547c478bd9Sstevel@tonic-gate 			if (status)
6557c478bd9Sstevel@tonic-gate 			    goto bad;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 	/*
6597c478bd9Sstevel@tonic-gate 	 * If we are doing a data compare, make sure the pattern
6607c478bd9Sstevel@tonic-gate 	 * came back intact.
6617c478bd9Sstevel@tonic-gate 	 * Only compare if we are on the read pass of SCAN_PURGE, or
6627c478bd9Sstevel@tonic-gate 	 * we wrote random data instead of the expected data pattern.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) {
6657c478bd9Sstevel@tonic-gate 		for (i = nints, ptr = (uint_t *)pattern_buf; i; i--)
6667c478bd9Sstevel@tonic-gate 			if (*ptr++ != data) {
6677c478bd9Sstevel@tonic-gate 				err_print("Data miscompare error (expecting ");
6687c478bd9Sstevel@tonic-gate 				err_print("0x%x, got 0x%x) at ", data,
6697c478bd9Sstevel@tonic-gate 					*((int *)((int *)pattern_buf +
6707c478bd9Sstevel@tonic-gate 					(nints - i))));
6717c478bd9Sstevel@tonic-gate 				pr_dblock(err_print, blkno);
672342440ecSPrasad Singamsetty 				err_print(", offset = 0x%llx.\n",
6737c478bd9Sstevel@tonic-gate 					(nints - i) * sizeof (int));
6747c478bd9Sstevel@tonic-gate 				goto bad;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	/*
6787c478bd9Sstevel@tonic-gate 	 * If we are supposed to write data out, do so.
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 	if (flags & SCAN_WRITE) {
6817c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6827c478bd9Sstevel@tonic-gate 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
6837c478bd9Sstevel@tonic-gate 		if (status)
6847c478bd9Sstevel@tonic-gate 			goto bad;
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	exit_critical();
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * No errors occurred, return ok.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	return (0);
6917c478bd9Sstevel@tonic-gate bad:
6927c478bd9Sstevel@tonic-gate 	/*
6937c478bd9Sstevel@tonic-gate 	 * There was an error.  If the data was corrupted, we write it
6947c478bd9Sstevel@tonic-gate 	 * out from the data buffer to restore it.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	if (corrupt) {
6977c478bd9Sstevel@tonic-gate 		if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6987c478bd9Sstevel@tonic-gate 				blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp))
6997c478bd9Sstevel@tonic-gate 		err_print("Warning: unable to restore original data.\n");
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	exit_critical();
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * Return the error.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	return (-1);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * This routine analyzes a set of sectors on the disk. It simply returns
7117c478bd9Sstevel@tonic-gate  * an error if a defect is found.  It is called by analyze_blocks().
7127c478bd9Sstevel@tonic-gate  * For simplicity, this is done as a separate function instead of
7137c478bd9Sstevel@tonic-gate  * making the analyze_block routine complex.
7147c478bd9Sstevel@tonic-gate  *
7157c478bd9Sstevel@tonic-gate  * This routine implements the 'verify' command.  It writes the disk
7167c478bd9Sstevel@tonic-gate  * by writing unique data for each block; after the write pass, it
7177c478bd9Sstevel@tonic-gate  * reads the data and verifies for correctness. Note that the entire
7187c478bd9Sstevel@tonic-gate  * disk (or the range of disk) is fully written first and then read.
7197c478bd9Sstevel@tonic-gate  * This should eliminate any caching effect on the drives.
7207c478bd9Sstevel@tonic-gate  */
7217c478bd9Sstevel@tonic-gate static int
verify_blocks(int flags,diskaddr_t blkno,uint_t blkcnt,unsigned data,int driver_flags,int * xfercntp)7227c478bd9Sstevel@tonic-gate verify_blocks(int flags,
7237c478bd9Sstevel@tonic-gate 		diskaddr_t blkno,
724342440ecSPrasad Singamsetty 		uint_t blkcnt,
7257c478bd9Sstevel@tonic-gate 		unsigned data,
7267c478bd9Sstevel@tonic-gate 		int driver_flags,
7277c478bd9Sstevel@tonic-gate 		int *xfercntp)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	int		status, i, nints;
7307c478bd9Sstevel@tonic-gate 	unsigned	*ptr = (uint_t *)pattern_buf;
7317c478bd9Sstevel@tonic-gate 
732*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	nints = cur_blksz / sizeof (int);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Initialize the pattern buffer if we are in write pass.
7367c478bd9Sstevel@tonic-gate 	 * Use the block number itself as data, each block has unique
7377c478bd9Sstevel@tonic-gate 	 * buffer data that way.
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
7407c478bd9Sstevel@tonic-gate 		for (data = blkno; data < blkno + blkcnt; data++) {
7417c478bd9Sstevel@tonic-gate 			for (i = 0; i < nints; i++) {
7427c478bd9Sstevel@tonic-gate 				*ptr++ = data;
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		ptr = (uint_t *)pattern_buf;
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * Only write if we're not on the read pass of SCAN_VERIFY.
7507c478bd9Sstevel@tonic-gate 	 */
7517c478bd9Sstevel@tonic-gate 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
7527c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
7537c478bd9Sstevel@tonic-gate 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
7547c478bd9Sstevel@tonic-gate 		if (status)
7557c478bd9Sstevel@tonic-gate 			goto bad;
7567c478bd9Sstevel@tonic-gate 	} else {
7577c478bd9Sstevel@tonic-gate 		/*
7587c478bd9Sstevel@tonic-gate 		 * Only read if we are on the read pass of SCAN_VERIFY
7597c478bd9Sstevel@tonic-gate 		 */
7607c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
7617c478bd9Sstevel@tonic-gate 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
7627c478bd9Sstevel@tonic-gate 		if (status)
7637c478bd9Sstevel@tonic-gate 			goto bad;
7647c478bd9Sstevel@tonic-gate 		/*
7657c478bd9Sstevel@tonic-gate 		 * compare and make sure the pattern came back intact.
7667c478bd9Sstevel@tonic-gate 		 */
7677c478bd9Sstevel@tonic-gate 		for (data = blkno; data < blkno + blkcnt; data++) {
7687c478bd9Sstevel@tonic-gate 			for (i = 0; i < nints; i++) {
7697c478bd9Sstevel@tonic-gate 				if (*ptr++ != data) {
7707c478bd9Sstevel@tonic-gate 					ptr--;
771342440ecSPrasad Singamsetty 					err_print("Data miscompare error "
772342440ecSPrasad Singamsetty 					    "(expecting 0x%x, got 0x%x) at ",
773342440ecSPrasad Singamsetty 					    data, *ptr);
7747c478bd9Sstevel@tonic-gate 					pr_dblock(err_print, blkno);
775342440ecSPrasad Singamsetty 					err_print(", offset = 0x%x.\n",
776342440ecSPrasad Singamsetty 					    (ptr - (uint_t *)pattern_buf) *
777342440ecSPrasad Singamsetty 					    sizeof (int));
7787c478bd9Sstevel@tonic-gate 					goto bad;
7797c478bd9Sstevel@tonic-gate 				}
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * No errors occurred, return ok.
7857c478bd9Sstevel@tonic-gate 	 */
7867c478bd9Sstevel@tonic-gate 	return (0);
7877c478bd9Sstevel@tonic-gate bad:
7887c478bd9Sstevel@tonic-gate 	return (-1);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate static int
handle_error_conditions()7937c478bd9Sstevel@tonic-gate handle_error_conditions()
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * Check if the errno is ENXIO.
7987c478bd9Sstevel@tonic-gate 	 */
7997c478bd9Sstevel@tonic-gate 	if (errno == ENXIO) {
8007c478bd9Sstevel@tonic-gate 		fmt_print("\n\nWarning:Cannot access drive, ");
8017c478bd9Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8027c478bd9Sstevel@tonic-gate 		return (-1);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * check for disk errors
8067c478bd9Sstevel@tonic-gate 	 */
8077c478bd9Sstevel@tonic-gate 	switch (disk_error) {
8087c478bd9Sstevel@tonic-gate 	case DISK_STAT_RESERVED:
8097c478bd9Sstevel@tonic-gate 	case DISK_STAT_UNAVAILABLE:
8107c478bd9Sstevel@tonic-gate 		fmt_print("\n\nWarning:Drive may be reserved ");
8117c478bd9Sstevel@tonic-gate 		fmt_print("or has been removed, ");
8127c478bd9Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8137c478bd9Sstevel@tonic-gate 		return (-1);
8147c478bd9Sstevel@tonic-gate 	case DISK_STAT_NOTREADY:
8157c478bd9Sstevel@tonic-gate 		fmt_print("\n\nWarning: Drive not ready, ");
8167c478bd9Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8177c478bd9Sstevel@tonic-gate 		return (-1);
8187c478bd9Sstevel@tonic-gate 	case DISK_STAT_DATA_PROTECT:
8197c478bd9Sstevel@tonic-gate 		fmt_print("\n\nWarning: Drive is write protected, ");
8207c478bd9Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8217c478bd9Sstevel@tonic-gate 		return (-1);
8227c478bd9Sstevel@tonic-gate 	default:
8237c478bd9Sstevel@tonic-gate 		break;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	return (0);
8267c478bd9Sstevel@tonic-gate }
827