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