xref: /onnv-gate/usr/src/cmd/format/analyze.c (revision 9889:68d0fe4c716e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55421Smishra  * Common Development and Distribution License (the "License").
65421Smishra  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9889SLarry.Liu@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This file contains routines to analyze the surface of a disk.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #include "global.h"
300Sstevel@tonic-gate #include "analyze.h"
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include "misc.h"
340Sstevel@tonic-gate #include "defect.h"
350Sstevel@tonic-gate #include "label.h"
360Sstevel@tonic-gate #include "param.h"
37767Ssjelinek #include "checkdev.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * These global variables control the surface analysis process.  They
420Sstevel@tonic-gate  * are set from a command in the defect menu.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate int	scan_entire = 1;		/* scan whole disk flag */
450Sstevel@tonic-gate diskaddr_t	scan_lower = 0;			/* lower bound */
460Sstevel@tonic-gate diskaddr_t	scan_upper = 0;			/* upper bound */
470Sstevel@tonic-gate int	scan_correct = 1;		/* correct errors flag */
480Sstevel@tonic-gate int	scan_stop = 0;			/* stop after error flag */
490Sstevel@tonic-gate int	scan_loop = 0;			/* loop forever flag */
500Sstevel@tonic-gate int	scan_passes = 2;		/* number of passes */
510Sstevel@tonic-gate int	scan_random = 0;		/* random patterns flag */
527563SPrasad.Singamsetty@Sun.COM uint_t	scan_size = 0;			/* sectors/scan operation */
530Sstevel@tonic-gate int	scan_auto = 1;			/* scan after format flag */
540Sstevel@tonic-gate int	scan_restore_defects = 1;	/* restore defect list after writing */
550Sstevel@tonic-gate int	scan_restore_label = 1;		/* restore label after writing */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * These are summary variables to print out info after analysis.
590Sstevel@tonic-gate  * Values less than 0 imply they are invalid.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate offset_t	scan_cur_block = -1;		/* current block */
620Sstevel@tonic-gate int64_t		scan_blocks_fixed = -1;		/* # blocks repaired */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * This variable is used to tell whether the most recent surface
660Sstevel@tonic-gate  * analysis error was caused by a media defect or some other problem.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate int	media_error;			/* error was caused by defect */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate int	disk_error;			/* disk errors during analysis */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * These are the data patterns used if random patterns are not chosen.
740Sstevel@tonic-gate  * They are designed to show pattern dependent errors.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static unsigned int	scan_patterns[] = {
770Sstevel@tonic-gate 	0xc6dec6de,
780Sstevel@tonic-gate 	0x6db6db6d,
790Sstevel@tonic-gate 	0x00000000,
800Sstevel@tonic-gate 	0xffffffff,
810Sstevel@tonic-gate 	0xaaaaaaaa,
820Sstevel@tonic-gate };
830Sstevel@tonic-gate #define	NPATTERNS	5		/* number of predefined patterns */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * These are the data patterns from the SunFed requirements document.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate static unsigned int purge_patterns[] = {	/* patterns to be written */
890Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
900Sstevel@tonic-gate 	0x55555555,		/* 01010101...  == UUUU... */
910Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
920Sstevel@tonic-gate 	0xaaaaaaaa,		/* 10101010... */
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static unsigned int alpha_pattern =  0x40404040;   /* 10000000...  == @@@@... */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /* Function prototypes */
980Sstevel@tonic-gate #ifdef	__STDC__
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static int	scan_repair(diskaddr_t bn, int mode);
1017563SPrasad.Singamsetty@Sun.COM static int	analyze_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
1020Sstevel@tonic-gate 		unsigned data, int init, int driver_flags, int *xfercntp);
1030Sstevel@tonic-gate static int	handle_error_conditions(void);
1047563SPrasad.Singamsetty@Sun.COM static int	verify_blocks(int flags, diskaddr_t blkno, uint_t blkcnt,
1050Sstevel@tonic-gate 		unsigned data, int driver_flags, int *xfercntp);
1060Sstevel@tonic-gate #else	/* __STDC__ */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static int	scan_repair();
1090Sstevel@tonic-gate static int	analyze_blocks();
1100Sstevel@tonic-gate static int	handle_error_conditions();
1110Sstevel@tonic-gate static int	verify_blocks();
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #endif	/* __STDC__ */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * This routine performs a surface analysis based upon the global
1170Sstevel@tonic-gate  * parameters.  It is called from several commands in the defect menu,
1180Sstevel@tonic-gate  * and from the format command in the command menu (if post-format
1190Sstevel@tonic-gate  * analysis is enable).
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate int
do_scan(flags,mode)1220Sstevel@tonic-gate do_scan(flags, mode)
1230Sstevel@tonic-gate 	int	flags, mode;
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	diskaddr_t	start, end, curnt;
1267563SPrasad.Singamsetty@Sun.COM 	int	pass, needinit, data;
1277563SPrasad.Singamsetty@Sun.COM 	uint_t	size;
1280Sstevel@tonic-gate 	int	status, founderr, i, j;
1290Sstevel@tonic-gate 	int	error = 0;
1300Sstevel@tonic-gate 	int	pattern = 0;
1310Sstevel@tonic-gate 	int	xfercnt;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * Check to be sure we aren't correcting without a defect list
1350Sstevel@tonic-gate 	 * if the controller can correct the defect.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if (scan_correct && !EMBEDDED_SCSI && (cur_ops->op_repair != NULL) &&
1380Sstevel@tonic-gate 			(cur_list.list == NULL)) {
1390Sstevel@tonic-gate 		err_print("Current Defect List must be initialized ");
1400Sstevel@tonic-gate 		err_print("to do automatic repair.\n");
1410Sstevel@tonic-gate 		return (-1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * Define the bounds of the scan.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	if (scan_entire) {
1470Sstevel@tonic-gate 		start = 0;
1480Sstevel@tonic-gate 	    if (cur_label == L_TYPE_SOLARIS) {
1490Sstevel@tonic-gate 		if (cur_ctype->ctype_flags & CF_SCSI)
1500Sstevel@tonic-gate 			end = datasects() - 1;
1510Sstevel@tonic-gate 		else
1520Sstevel@tonic-gate 			end = physsects() - 1;
1530Sstevel@tonic-gate 	    } else if (cur_label == L_TYPE_EFI) {
1540Sstevel@tonic-gate 		end = cur_parts->etoc->efi_last_lba;
1550Sstevel@tonic-gate 	    }
1560Sstevel@tonic-gate 	} else {
1570Sstevel@tonic-gate 		start = scan_lower;
1580Sstevel@tonic-gate 		end = scan_upper;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Make sure the user knows if we are scanning over a mounted
1620Sstevel@tonic-gate 	 * partition.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
1650Sstevel@tonic-gate 	    (checkmount(start, end))) {
1660Sstevel@tonic-gate 		err_print("Cannot do analysis on a mounted partition.\n");
1670Sstevel@tonic-gate 		return (-1);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Make sure the user knows if we are scanning over a
1720Sstevel@tonic-gate 	 * partition being used for swapping.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
1750Sstevel@tonic-gate 	    (checkswap(start, end))) {
1760Sstevel@tonic-gate 		err_print("Cannot do analysis on a partition \
1775421Smishra 		    which is currently being used for swapping.\n");
1785421Smishra 		return (-1);
1795421Smishra 	}
1805421Smishra 
1815421Smishra 	/*
1825421Smishra 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
1835421Smishra 	 * or live upgrade are on the disk.
1845421Smishra 	 */
1855421Smishra 	if ((flags & (SCAN_PATTERN | SCAN_WRITE)) &&
1865421Smishra 	    (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
1875421Smishra 	    (diskaddr_t)-1, 0, 0))) {
1885421Smishra 		err_print("Cannot do analysis on a partition "
1895421Smishra 		    "while it in use as described above.\n");
1900Sstevel@tonic-gate 		return (-1);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * If we are scanning destructively over certain sectors,
1950Sstevel@tonic-gate 	 * we mark the defect list and/or label dirty so it will get rewritten.
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	if (flags & (SCAN_PATTERN | SCAN_WRITE)) {
1980Sstevel@tonic-gate 	    if (cur_label == L_TYPE_SOLARIS) {
1997563SPrasad.Singamsetty@Sun.COM 		if (start < (diskaddr_t)totalsects() &&
2007563SPrasad.Singamsetty@Sun.COM 				end >= (diskaddr_t)datasects()) {
2010Sstevel@tonic-gate 			if (!EMBEDDED_SCSI) {
2020Sstevel@tonic-gate 				cur_list.flags |= LIST_DIRTY;
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 			if (cur_disk->disk_flags & DSK_LABEL)
2050Sstevel@tonic-gate 				cur_flags |= LABEL_DIRTY;
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	    }
2080Sstevel@tonic-gate 	    if (start == 0) {
2090Sstevel@tonic-gate 		if (cur_disk->disk_flags & DSK_LABEL)
2100Sstevel@tonic-gate 			cur_flags |= LABEL_DIRTY;
2110Sstevel@tonic-gate 	    }
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Initialize the summary info on sectors repaired.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	scan_blocks_fixed = 0;
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * Loop through the passes of the scan. If required, loop forever.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	for (pass = 0; pass < scan_passes || scan_loop; pass++) {
2210Sstevel@tonic-gate 		/*
2220Sstevel@tonic-gate 		 * Determine the data pattern to use if pattern testing
2230Sstevel@tonic-gate 		 * is to be done.
2240Sstevel@tonic-gate 		 */
2250Sstevel@tonic-gate 		if (flags & SCAN_PATTERN) {
2260Sstevel@tonic-gate 			if (scan_random)
2270Sstevel@tonic-gate 				data = (int)mrand48();
2280Sstevel@tonic-gate 			else
2290Sstevel@tonic-gate 				data = scan_patterns[pass % NPPATTERNS];
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 			if (flags & SCAN_PURGE) {
2320Sstevel@tonic-gate 				flags &= ~(SCAN_PURGE_READ_PASS
2330Sstevel@tonic-gate 						| SCAN_PURGE_ALPHA_PASS);
2340Sstevel@tonic-gate 				switch (pattern % (NPPATTERNS + 1)) {
2350Sstevel@tonic-gate 				case NPPATTERNS:
2360Sstevel@tonic-gate 					pattern = 0;
2370Sstevel@tonic-gate 					if (!error) {
2380Sstevel@tonic-gate 					    fmt_print(
2390Sstevel@tonic-gate "\nThe last %d passes were successful, running alpha pattern pass", NPPATTERNS);
2400Sstevel@tonic-gate 					    flags |= SCAN_PURGE_ALPHA_PASS;
2410Sstevel@tonic-gate 					    data = alpha_pattern;
2420Sstevel@tonic-gate 					} else {
2430Sstevel@tonic-gate 					    data = purge_patterns[pattern];
2440Sstevel@tonic-gate 					    pattern++;
2450Sstevel@tonic-gate 					};
2460Sstevel@tonic-gate 					break;
2470Sstevel@tonic-gate 				case READPATTERN:
2480Sstevel@tonic-gate 					flags |=  SCAN_PURGE_READ_PASS;
2490Sstevel@tonic-gate 				default:
2500Sstevel@tonic-gate 					data = purge_patterns[pattern];
2510Sstevel@tonic-gate 					pattern++;
2520Sstevel@tonic-gate 					break;
2530Sstevel@tonic-gate 				}
2540Sstevel@tonic-gate 			}
2550Sstevel@tonic-gate 			fmt_print("\n        pass %d", pass);
2560Sstevel@tonic-gate 			fmt_print(" - pattern = 0x%x", data);
2570Sstevel@tonic-gate 		} else
2580Sstevel@tonic-gate 			fmt_print("\n        pass %d", pass);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		fmt_print("\n");
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * Mark the pattern buffer as corrupt, since it
2630Sstevel@tonic-gate 		 * hasn't been initialized.
2640Sstevel@tonic-gate 		 */
2650Sstevel@tonic-gate 		needinit = 1;
2660Sstevel@tonic-gate 		/*
2670Sstevel@tonic-gate 		 * Print the first block number to the log file if
2680Sstevel@tonic-gate 		 * logging is on so there is some record of what
2690Sstevel@tonic-gate 		 * analysis was performed.
2700Sstevel@tonic-gate 		 */
2710Sstevel@tonic-gate 		if (log_file) {
2720Sstevel@tonic-gate 			pr_dblock(log_print, start);
2730Sstevel@tonic-gate 			log_print("\n");
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * Loop through this pass, each time analyzing an amount
2770Sstevel@tonic-gate 		 * specified by the global parameters.
2780Sstevel@tonic-gate 		 */
2790Sstevel@tonic-gate 		xfercnt = 0;
2800Sstevel@tonic-gate 		for (curnt = start; curnt <= end; curnt += size) {
2810Sstevel@tonic-gate 			if ((end - curnt) < scan_size)
2820Sstevel@tonic-gate 				size = end - curnt + 1;
2830Sstevel@tonic-gate 			else
2840Sstevel@tonic-gate 				size = scan_size;
2850Sstevel@tonic-gate 			/*
2860Sstevel@tonic-gate 			 * Print out where we are, so we don't look dead.
2870Sstevel@tonic-gate 			 * Also store it in summary info for logging.
2880Sstevel@tonic-gate 			 */
2890Sstevel@tonic-gate 			scan_cur_block = curnt;
2900Sstevel@tonic-gate 			nolog_print("   ");
2910Sstevel@tonic-gate 			pr_dblock(nolog_print, curnt);
2920Sstevel@tonic-gate 			nolog_print("  \015");
2930Sstevel@tonic-gate 			(void) fflush(stdout);
2940Sstevel@tonic-gate 			disk_error = 0;
2950Sstevel@tonic-gate 			/*
2960Sstevel@tonic-gate 			 * Do the actual analysis.
2970Sstevel@tonic-gate 			 */
2986781Sny155746 			status = analyze_blocks(flags, curnt, size,
2990Sstevel@tonic-gate 			    (unsigned)data, needinit, (F_ALLERRS | F_SILENT),
3000Sstevel@tonic-gate 			    &xfercnt);
3010Sstevel@tonic-gate 			/*
3020Sstevel@tonic-gate 			 * If there were no errors, the pattern buffer is
3030Sstevel@tonic-gate 			 * still initialized, and we just loop to next chunk.
3040Sstevel@tonic-gate 			 */
3050Sstevel@tonic-gate 			needinit = 0;
3060Sstevel@tonic-gate 			if (!status)
3070Sstevel@tonic-gate 				continue;
3080Sstevel@tonic-gate 			/*
3090Sstevel@tonic-gate 			 * There was an error. Check if surface analysis
3100Sstevel@tonic-gate 			 * can be continued.
3110Sstevel@tonic-gate 			 */
3120Sstevel@tonic-gate 			if (handle_error_conditions()) {
3130Sstevel@tonic-gate 				scan_blocks_fixed = scan_cur_block = -1;
3140Sstevel@tonic-gate 				return (-1);
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 			/*
3170Sstevel@tonic-gate 			 * There was an error. Mark the pattern buffer
3180Sstevel@tonic-gate 			 * corrupt so it will get reinitialized.
3190Sstevel@tonic-gate 			 */
3200Sstevel@tonic-gate 			needinit = 1;
3210Sstevel@tonic-gate 			/*
3220Sstevel@tonic-gate 			 * If it was not a media error, ignore it.
3230Sstevel@tonic-gate 			 */
3240Sstevel@tonic-gate 			if (!media_error)
3250Sstevel@tonic-gate 				continue;
3260Sstevel@tonic-gate 			/*
3270Sstevel@tonic-gate 			 * Loop 5 times through each sector of the chunk,
3280Sstevel@tonic-gate 			 * analyzing them individually.
3290Sstevel@tonic-gate 			 */
3300Sstevel@tonic-gate 			nolog_print("   ");
3310Sstevel@tonic-gate 			pr_dblock(nolog_print, curnt);
3320Sstevel@tonic-gate 			nolog_print("  \015");
3330Sstevel@tonic-gate 			(void) fflush(stdout);
3340Sstevel@tonic-gate 			founderr = 0;
3350Sstevel@tonic-gate 			for (j = 0; j < size * 5; j++) {
3360Sstevel@tonic-gate 				i = j % size;
3370Sstevel@tonic-gate 				disk_error = 0;
3386781Sny155746 				status = analyze_blocks(flags, (curnt + i), 1,
3396781Sny155746 				    (unsigned)data, needinit, F_ALLERRS, NULL);
3400Sstevel@tonic-gate 				needinit = 0;
3410Sstevel@tonic-gate 				if (!status)
3420Sstevel@tonic-gate 					continue;
3430Sstevel@tonic-gate 				/*
3440Sstevel@tonic-gate 				 * There was an error. Check if surface analysis
3450Sstevel@tonic-gate 				 * can be continued.
3460Sstevel@tonic-gate 				 */
3470Sstevel@tonic-gate 				if (handle_error_conditions()) {
3480Sstevel@tonic-gate 					scan_blocks_fixed = scan_cur_block = -1;
3490Sstevel@tonic-gate 					return (-1);
3500Sstevel@tonic-gate 				}
3510Sstevel@tonic-gate 				/*
3520Sstevel@tonic-gate 				 * An error occurred.  Mark the buffer
3530Sstevel@tonic-gate 				 * corrupt and see if it was media
3540Sstevel@tonic-gate 				 * related.
3550Sstevel@tonic-gate 				 */
3560Sstevel@tonic-gate 				needinit = 1;
3570Sstevel@tonic-gate 				if (!media_error)
3580Sstevel@tonic-gate 					continue;
3590Sstevel@tonic-gate 				/*
3600Sstevel@tonic-gate 				 * We found a bad sector. Print out a message
3610Sstevel@tonic-gate 				 * and fix it if required.
3620Sstevel@tonic-gate 				 */
3630Sstevel@tonic-gate 				founderr = 1;
3640Sstevel@tonic-gate 				if (scan_correct && (flags != SCAN_VALID)) {
3650Sstevel@tonic-gate 					if (scan_repair(curnt+i, mode)) {
3660Sstevel@tonic-gate 						error = -1;
3670Sstevel@tonic-gate 					}
3680Sstevel@tonic-gate 				} else
3690Sstevel@tonic-gate 					err_print("\n");
3700Sstevel@tonic-gate 				/*
3710Sstevel@tonic-gate 				 * Stop after the error if required.
3720Sstevel@tonic-gate 				 */
3730Sstevel@tonic-gate 				if (scan_stop)
3740Sstevel@tonic-gate 					goto out;
3750Sstevel@tonic-gate 			}
3760Sstevel@tonic-gate 			/*
3770Sstevel@tonic-gate 			 * Mark the pattern buffer corrupt to be safe.
3780Sstevel@tonic-gate 			 */
3790Sstevel@tonic-gate 			needinit = 1;
3800Sstevel@tonic-gate 			/*
3810Sstevel@tonic-gate 			 * We didn't find an individual sector that was bad.
3820Sstevel@tonic-gate 			 * Print out a warning.
3830Sstevel@tonic-gate 			 */
3840Sstevel@tonic-gate 			if (!founderr) {
3850Sstevel@tonic-gate 				err_print("Warning: unable to pinpoint ");
3860Sstevel@tonic-gate 				err_print("defective block.\n");
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 		/*
3900Sstevel@tonic-gate 		 * Print the end of each pass to the log file.
3910Sstevel@tonic-gate 		 */
3920Sstevel@tonic-gate 		enter_critical();
3930Sstevel@tonic-gate 		if (log_file) {
3940Sstevel@tonic-gate 			pr_dblock(log_print, scan_cur_block);
3950Sstevel@tonic-gate 			log_print("\n");
3960Sstevel@tonic-gate 		}
3970Sstevel@tonic-gate 		scan_cur_block = -1;
3980Sstevel@tonic-gate 		exit_critical();
3990Sstevel@tonic-gate 		fmt_print("\n");
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		/*
4020Sstevel@tonic-gate 		 * alternate the read and write for SCAN_VERIFY test
4030Sstevel@tonic-gate 		 */
4040Sstevel@tonic-gate 		if (flags & SCAN_VERIFY) {
4050Sstevel@tonic-gate 			flags ^= SCAN_VERIFY_READ_PASS;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate out:
4090Sstevel@tonic-gate 	/*
4100Sstevel@tonic-gate 	 * We got here either by giving up after an error or falling
4110Sstevel@tonic-gate 	 * through after all passes were completed.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	fmt_print("\n");
4140Sstevel@tonic-gate 	enter_critical();
4150Sstevel@tonic-gate 	/*
4160Sstevel@tonic-gate 	 * If the defect list is dirty, write it to disk,
4170Sstevel@tonic-gate 	 * if scan_restore_defects (the default) is true.
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	if (!EMBEDDED_SCSI && (cur_list.flags & LIST_DIRTY) &&
4200Sstevel@tonic-gate 				(scan_restore_defects)) {
4210Sstevel@tonic-gate 		cur_list.flags = 0;
4220Sstevel@tonic-gate 		write_deflist(&cur_list);
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * If the label is dirty, write it to disk.
4260Sstevel@tonic-gate 	 * if scan_restore_label (the default) is true.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	if ((cur_flags & LABEL_DIRTY) && (scan_restore_label)) {
4290Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
4300Sstevel@tonic-gate 		(void) write_label();
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 	/*
4330Sstevel@tonic-gate 	 * If we dropped down to here after an error, we need to write
4340Sstevel@tonic-gate 	 * the final block number to the log file for record keeping.
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	if (log_file && scan_cur_block >= 0) {
4370Sstevel@tonic-gate 		pr_dblock(log_print, scan_cur_block);
4380Sstevel@tonic-gate 		log_print("\n");
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	fmt_print("Total of %lld defective blocks repaired.\n",
4410Sstevel@tonic-gate 		scan_blocks_fixed);
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * Reinitialize the logging variables so they don't get used
4440Sstevel@tonic-gate 	 * when they are not really valid.
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	scan_blocks_fixed = scan_cur_block = -1;
4470Sstevel@tonic-gate 	exit_critical();
4480Sstevel@tonic-gate 	return (error);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * This routine is called to repair a bad block discovered
4540Sstevel@tonic-gate  * during a scan operation.  Return 0 for success, 1 for failure.
4550Sstevel@tonic-gate  * (This has been extracted out of do_scan(), to simplify it.)
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate static int
scan_repair(bn,mode)4580Sstevel@tonic-gate scan_repair(bn, mode)
4590Sstevel@tonic-gate 	diskaddr_t	bn;
4600Sstevel@tonic-gate 	int	mode;
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	int	status;
4630Sstevel@tonic-gate 	int	result = 1;
464*9889SLarry.Liu@Sun.COM 	char	*buf;
4650Sstevel@tonic-gate 	int	buf_is_good;
4660Sstevel@tonic-gate 	int	i;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (cur_ops->op_repair == NULL) {
4690Sstevel@tonic-gate 		err_print("Warning: Controller does ");
4700Sstevel@tonic-gate 		err_print("not support repairing.\n\n");
4710Sstevel@tonic-gate 		return (result);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
474*9889SLarry.Liu@Sun.COM 	buf = malloc(cur_blksz);
475*9889SLarry.Liu@Sun.COM 	if (buf == NULL) {
476*9889SLarry.Liu@Sun.COM 		err_print("Warning: no memory.\n\n");
477*9889SLarry.Liu@Sun.COM 		return (result);
478*9889SLarry.Liu@Sun.COM 	}
4790Sstevel@tonic-gate 	enter_critical();
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * Determine if the error appears to be hard or soft.  We
4830Sstevel@tonic-gate 	 * already assume there's an error.  If we can get any
4840Sstevel@tonic-gate 	 * good data out of the sector, write that data back
4850Sstevel@tonic-gate 	 * after the repair.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	buf_is_good = 0;
4880Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
4890Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1,
4900Sstevel@tonic-gate 				buf, F_SILENT, NULL);
4910Sstevel@tonic-gate 		if (status == 0) {
4920Sstevel@tonic-gate 			buf_is_good = 1;
4930Sstevel@tonic-gate 			break;
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	fmt_print("Repairing %s error on %llu (",
4980Sstevel@tonic-gate 				buf_is_good ? "soft" : "hard", bn);
4990Sstevel@tonic-gate 	pr_dblock(fmt_print, bn);
5000Sstevel@tonic-gate 	fmt_print(")...");
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	status = (*cur_ops->op_repair)(bn, mode);
5030Sstevel@tonic-gate 	if (status) {
5040Sstevel@tonic-gate 		/*
5050Sstevel@tonic-gate 		 * If the repair failed, we note it and will return the
5060Sstevel@tonic-gate 		 * failure. However, the analysis goes on.
5070Sstevel@tonic-gate 		 */
5080Sstevel@tonic-gate 		fmt_print("failed.\n\n");
5090Sstevel@tonic-gate 	} else {
5100Sstevel@tonic-gate 		/*
5110Sstevel@tonic-gate 		 * The repair worked.  Write the good data we could
5120Sstevel@tonic-gate 		 * recover from the failed block, if possible.
5130Sstevel@tonic-gate 		 * If not, zero the block.  In doing so, try to
5140Sstevel@tonic-gate 		 * determine if the new block appears ok.
5150Sstevel@tonic-gate 		 */
5160Sstevel@tonic-gate 		if (!buf_is_good) {
517*9889SLarry.Liu@Sun.COM 			bzero(buf, cur_blksz);
5180Sstevel@tonic-gate 			fmt_print("Warning: Block %llu zero-filled.\n", bn);
5190Sstevel@tonic-gate 		} else {
5200Sstevel@tonic-gate 			fmt_print("ok.\n");
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
5230Sstevel@tonic-gate 					1, buf, (F_SILENT | F_ALLERRS), NULL);
5240Sstevel@tonic-gate 		if (status == 0) {
5250Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
5260Sstevel@tonic-gate 					1, buf, (F_SILENT | F_ALLERRS), NULL);
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 		if (status) {
5290Sstevel@tonic-gate 			fmt_print("The new block also appears defective.\n");
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 		fmt_print("\n");
5320Sstevel@tonic-gate 		/*
5330Sstevel@tonic-gate 		 * add the defect to the list and write the list out.
5340Sstevel@tonic-gate 		 * Also, kill the working list so it will get resynced
5350Sstevel@tonic-gate 		 * with the current list.
5360Sstevel@tonic-gate 		 *
5370Sstevel@tonic-gate 		 * For embedded scsi, we don't require a defect list.
5380Sstevel@tonic-gate 		 * However, if we have one, add the defect if the
5390Sstevel@tonic-gate 		 * list includes the grown list.  If not, kill it
5400Sstevel@tonic-gate 		 * to force a resync if we need the list later.
5410Sstevel@tonic-gate 		 */
5420Sstevel@tonic-gate 		if (EMBEDDED_SCSI) {
5430Sstevel@tonic-gate 			if (cur_list.list != NULL) {
5440Sstevel@tonic-gate 				if (cur_list.flags & LIST_PGLIST) {
5450Sstevel@tonic-gate 					add_ldef(bn, &cur_list);
5460Sstevel@tonic-gate 				} else {
5470Sstevel@tonic-gate 					kill_deflist(&cur_list);
5480Sstevel@tonic-gate 				}
5490Sstevel@tonic-gate 			}
5500Sstevel@tonic-gate 		/*
5510Sstevel@tonic-gate 		 * The next "if" statement reflects the fix for
5520Sstevel@tonic-gate 		 * bug id 1026096 where format keeps adding the
5530Sstevel@tonic-gate 		 * same defect to the defect list.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
5560Sstevel@tonic-gate 			kill_deflist(&cur_list);
5570Sstevel@tonic-gate 			(*cur_ops->op_ex_cur)(&cur_list);
5580Sstevel@tonic-gate 			fmt_print("Current list updated\n");
5590Sstevel@tonic-gate 		} else {
5600Sstevel@tonic-gate 			add_ldef(bn, &cur_list);
5610Sstevel@tonic-gate 			write_deflist(&cur_list);
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		kill_deflist(&work_list);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		/* Log the repair.  */
5660Sstevel@tonic-gate 		scan_blocks_fixed++;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 		/* return ok */
5690Sstevel@tonic-gate 		result = 0;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	exit_critical();
573*9889SLarry.Liu@Sun.COM 	free(buf);
5740Sstevel@tonic-gate 	return (result);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * This routine analyzes a set of sectors on the disk.  It simply returns
5800Sstevel@tonic-gate  * an error if a defect is found.  It is called by do_scan().
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate static int
analyze_blocks(flags,blkno,blkcnt,data,init,driver_flags,xfercntp)5830Sstevel@tonic-gate analyze_blocks(flags, blkno, blkcnt, data, init, driver_flags, xfercntp)
5847563SPrasad.Singamsetty@Sun.COM 	int	flags, driver_flags, init;
5857563SPrasad.Singamsetty@Sun.COM 	uint_t	blkcnt;
5860Sstevel@tonic-gate 	register unsigned data;
5870Sstevel@tonic-gate 	diskaddr_t	blkno;
5880Sstevel@tonic-gate 	int	*xfercntp;
5890Sstevel@tonic-gate {
5907563SPrasad.Singamsetty@Sun.COM 	int		corrupt = 0;
5917563SPrasad.Singamsetty@Sun.COM 	int		status;
5927563SPrasad.Singamsetty@Sun.COM 	register diskaddr_t	i, nints;
5930Sstevel@tonic-gate 	register unsigned *ptr = (uint_t *)pattern_buf;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	media_error = 0;
5960Sstevel@tonic-gate 	if (flags & SCAN_VERIFY) {
5970Sstevel@tonic-gate 		return (verify_blocks(flags, blkno, blkcnt, data,
5980Sstevel@tonic-gate 		    driver_flags, xfercntp));
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * Initialize the pattern buffer if necessary.
6030Sstevel@tonic-gate 	 */
604*9889SLarry.Liu@Sun.COM 	nints = (diskaddr_t)blkcnt * cur_blksz / sizeof (int);
6050Sstevel@tonic-gate 	if ((flags & SCAN_PATTERN) && init) {
6060Sstevel@tonic-gate 		for (i = 0; i < nints; i++)
6070Sstevel@tonic-gate 			*((int *)((int *)pattern_buf + i)) = data;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * Lock out interrupts so we can insure valid data will get
6110Sstevel@tonic-gate 	 * restored. This is necessary because there are modes
6120Sstevel@tonic-gate 	 * of scanning that corrupt the disk data then restore it at
6130Sstevel@tonic-gate 	 * the end of the analysis.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	enter_critical();
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * If the disk data is valid, read it into the data buffer.
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate 	if (flags & SCAN_VALID) {
6200Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
6210Sstevel@tonic-gate 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
6220Sstevel@tonic-gate 		if (status)
6230Sstevel@tonic-gate 			goto bad;
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * If we are doing pattern testing, write and read the pattern
6270Sstevel@tonic-gate 	 * from the pattern buffer.
6280Sstevel@tonic-gate 	 */
6290Sstevel@tonic-gate 	if (flags & SCAN_PATTERN) {
6300Sstevel@tonic-gate 		/*
6310Sstevel@tonic-gate 		 * If the disk data was valid, mark it corrupt so we know
6320Sstevel@tonic-gate 		 * to restore it later.
6330Sstevel@tonic-gate 		 */
6340Sstevel@tonic-gate 		if (flags & SCAN_VALID)
6350Sstevel@tonic-gate 			corrupt++;
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * Only write if we're not on the read pass of SCAN_PURGE.
6380Sstevel@tonic-gate 		 */
6390Sstevel@tonic-gate 		if (!(flags & SCAN_PURGE_READ_PASS)) {
6400Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6410Sstevel@tonic-gate 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
6420Sstevel@tonic-gate 			    xfercntp);
6430Sstevel@tonic-gate 			if (status)
6440Sstevel@tonic-gate 			    goto bad;
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 		/*
6470Sstevel@tonic-gate 		 * Only read if we are on the read pass of SCAN_PURGE, if we
6480Sstevel@tonic-gate 		 * are purging.
6490Sstevel@tonic-gate 		 */
6500Sstevel@tonic-gate 		if ((!(flags & SCAN_PURGE)) || (flags & SCAN_PURGE_READ_PASS)) {
6510Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
6520Sstevel@tonic-gate 			    blkcnt, (caddr_t)pattern_buf, driver_flags,
6530Sstevel@tonic-gate 			    xfercntp);
6540Sstevel@tonic-gate 			if (status)
6550Sstevel@tonic-gate 			    goto bad;
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * If we are doing a data compare, make sure the pattern
6600Sstevel@tonic-gate 	 * came back intact.
6610Sstevel@tonic-gate 	 * Only compare if we are on the read pass of SCAN_PURGE, or
6620Sstevel@tonic-gate 	 * we wrote random data instead of the expected data pattern.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	if ((flags & SCAN_COMPARE) || (flags & SCAN_PURGE_READ_PASS)) {
6650Sstevel@tonic-gate 		for (i = nints, ptr = (uint_t *)pattern_buf; i; i--)
6660Sstevel@tonic-gate 			if (*ptr++ != data) {
6670Sstevel@tonic-gate 				err_print("Data miscompare error (expecting ");
6680Sstevel@tonic-gate 				err_print("0x%x, got 0x%x) at ", data,
6690Sstevel@tonic-gate 					*((int *)((int *)pattern_buf +
6700Sstevel@tonic-gate 					(nints - i))));
6710Sstevel@tonic-gate 				pr_dblock(err_print, blkno);
6727563SPrasad.Singamsetty@Sun.COM 				err_print(", offset = 0x%llx.\n",
6730Sstevel@tonic-gate 					(nints - i) * sizeof (int));
6740Sstevel@tonic-gate 				goto bad;
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * If we are supposed to write data out, do so.
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	if (flags & SCAN_WRITE) {
6810Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6820Sstevel@tonic-gate 		    blkcnt, (caddr_t)cur_buf, driver_flags, xfercntp);
6830Sstevel@tonic-gate 		if (status)
6840Sstevel@tonic-gate 			goto bad;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	exit_critical();
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * No errors occurred, return ok.
6890Sstevel@tonic-gate 	 */
6900Sstevel@tonic-gate 	return (0);
6910Sstevel@tonic-gate bad:
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * There was an error.  If the data was corrupted, we write it
6940Sstevel@tonic-gate 	 * out from the data buffer to restore it.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	if (corrupt) {
6970Sstevel@tonic-gate 		if ((*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
6980Sstevel@tonic-gate 				blkcnt, (caddr_t)cur_buf, F_NORMAL, xfercntp))
6990Sstevel@tonic-gate 		err_print("Warning: unable to restore original data.\n");
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 	exit_critical();
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * Return the error.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	return (-1);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate  * This routine analyzes a set of sectors on the disk. It simply returns
7110Sstevel@tonic-gate  * an error if a defect is found.  It is called by analyze_blocks().
7120Sstevel@tonic-gate  * For simplicity, this is done as a separate function instead of
7130Sstevel@tonic-gate  * making the analyze_block routine complex.
7140Sstevel@tonic-gate  *
7150Sstevel@tonic-gate  * This routine implements the 'verify' command.  It writes the disk
7160Sstevel@tonic-gate  * by writing unique data for each block; after the write pass, it
7170Sstevel@tonic-gate  * reads the data and verifies for correctness. Note that the entire
7180Sstevel@tonic-gate  * disk (or the range of disk) is fully written first and then read.
7190Sstevel@tonic-gate  * This should eliminate any caching effect on the drives.
7200Sstevel@tonic-gate  */
7210Sstevel@tonic-gate static int
verify_blocks(int flags,diskaddr_t blkno,uint_t blkcnt,unsigned data,int driver_flags,int * xfercntp)7220Sstevel@tonic-gate verify_blocks(int flags,
7230Sstevel@tonic-gate 		diskaddr_t blkno,
7247563SPrasad.Singamsetty@Sun.COM 		uint_t blkcnt,
7250Sstevel@tonic-gate 		unsigned data,
7260Sstevel@tonic-gate 		int driver_flags,
7270Sstevel@tonic-gate 		int *xfercntp)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	int		status, i, nints;
7300Sstevel@tonic-gate 	unsigned	*ptr = (uint_t *)pattern_buf;
7310Sstevel@tonic-gate 
732*9889SLarry.Liu@Sun.COM 	nints = cur_blksz / sizeof (int);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * Initialize the pattern buffer if we are in write pass.
7360Sstevel@tonic-gate 	 * Use the block number itself as data, each block has unique
7370Sstevel@tonic-gate 	 * buffer data that way.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
7400Sstevel@tonic-gate 		for (data = blkno; data < blkno + blkcnt; data++) {
7410Sstevel@tonic-gate 			for (i = 0; i < nints; i++) {
7420Sstevel@tonic-gate 				*ptr++ = data;
7430Sstevel@tonic-gate 			}
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 		ptr = (uint_t *)pattern_buf;
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * Only write if we're not on the read pass of SCAN_VERIFY.
7500Sstevel@tonic-gate 	 */
7510Sstevel@tonic-gate 	if (!(flags & SCAN_VERIFY_READ_PASS)) {
7520Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, blkno,
7530Sstevel@tonic-gate 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
7540Sstevel@tonic-gate 		if (status)
7550Sstevel@tonic-gate 			goto bad;
7560Sstevel@tonic-gate 	} else {
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * Only read if we are on the read pass of SCAN_VERIFY
7590Sstevel@tonic-gate 		 */
7600Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, blkno,
7610Sstevel@tonic-gate 		    blkcnt, (caddr_t)pattern_buf, driver_flags, xfercntp);
7620Sstevel@tonic-gate 		if (status)
7630Sstevel@tonic-gate 			goto bad;
7640Sstevel@tonic-gate 		/*
7650Sstevel@tonic-gate 		 * compare and make sure the pattern came back intact.
7660Sstevel@tonic-gate 		 */
7670Sstevel@tonic-gate 		for (data = blkno; data < blkno + blkcnt; data++) {
7687563SPrasad.Singamsetty@Sun.COM 			for (i = 0; i < nints; i++) {
7697563SPrasad.Singamsetty@Sun.COM 				if (*ptr++ != data) {
7707563SPrasad.Singamsetty@Sun.COM 					ptr--;
7717563SPrasad.Singamsetty@Sun.COM 					err_print("Data miscompare error "
7727563SPrasad.Singamsetty@Sun.COM 					    "(expecting 0x%x, got 0x%x) at ",
7737563SPrasad.Singamsetty@Sun.COM 					    data, *ptr);
7747563SPrasad.Singamsetty@Sun.COM 					pr_dblock(err_print, blkno);
7757563SPrasad.Singamsetty@Sun.COM 					err_print(", offset = 0x%x.\n",
7767563SPrasad.Singamsetty@Sun.COM 					    (ptr - (uint_t *)pattern_buf) *
7777563SPrasad.Singamsetty@Sun.COM 					    sizeof (int));
7787563SPrasad.Singamsetty@Sun.COM 					goto bad;
7797563SPrasad.Singamsetty@Sun.COM 				}
7800Sstevel@tonic-gate 			}
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * No errors occurred, return ok.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	return (0);
7870Sstevel@tonic-gate bad:
7880Sstevel@tonic-gate 	return (-1);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate static int
handle_error_conditions()7930Sstevel@tonic-gate handle_error_conditions()
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * Check if the errno is ENXIO.
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	if (errno == ENXIO) {
8000Sstevel@tonic-gate 		fmt_print("\n\nWarning:Cannot access drive, ");
8010Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8020Sstevel@tonic-gate 		return (-1);
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * check for disk errors
8060Sstevel@tonic-gate 	 */
8070Sstevel@tonic-gate 	switch (disk_error) {
8080Sstevel@tonic-gate 	case DISK_STAT_RESERVED:
8090Sstevel@tonic-gate 	case DISK_STAT_UNAVAILABLE:
8100Sstevel@tonic-gate 		fmt_print("\n\nWarning:Drive may be reserved ");
8110Sstevel@tonic-gate 		fmt_print("or has been removed, ");
8120Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8130Sstevel@tonic-gate 		return (-1);
8140Sstevel@tonic-gate 	case DISK_STAT_NOTREADY:
8150Sstevel@tonic-gate 		fmt_print("\n\nWarning: Drive not ready, ");
8160Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8170Sstevel@tonic-gate 		return (-1);
8180Sstevel@tonic-gate 	case DISK_STAT_DATA_PROTECT:
8190Sstevel@tonic-gate 		fmt_print("\n\nWarning: Drive is write protected, ");
8200Sstevel@tonic-gate 		fmt_print("aborting surface analysis.\n");
8210Sstevel@tonic-gate 		return (-1);
8220Sstevel@tonic-gate 	default:
8230Sstevel@tonic-gate 		break;
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 	return (0);
8260Sstevel@tonic-gate }
827