xref: /onnv-gate/usr/src/cmd/addbadsec/addbadsec.c (revision 7821:144c1a7abd32)
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
5*7821SMark.Logan@Sun.COM  * Common Development and Distribution License (the "License").
6*7821SMark.Logan@Sun.COM  * 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*7821SMark.Logan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Copyrighted as an unpublished work.
320Sstevel@tonic-gate  * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
330Sstevel@tonic-gate  * All rights reserved.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <memory.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
430Sstevel@tonic-gate #include <sys/vtoc.h>
440Sstevel@tonic-gate #include <sys/dkio.h>
450Sstevel@tonic-gate #include <errno.h>
46*7821SMark.Logan@Sun.COM #include <stdlib.h>
47*7821SMark.Logan@Sun.COM #include <strings.h>
48*7821SMark.Logan@Sun.COM #include <unistd.h>
49*7821SMark.Logan@Sun.COM #include <stropts.h>
500Sstevel@tonic-gate #include <sys/scsi/generic/commands.h>
510Sstevel@tonic-gate #include <sys/scsi/impl/commands.h>
520Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
530Sstevel@tonic-gate #include "badsec.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate char    *devname;		/* name of device */
560Sstevel@tonic-gate int	devfd;			/* device file descriptor */
57*7821SMark.Logan@Sun.COM struct	dk_geom		dkg;	/* geometry */
58*7821SMark.Logan@Sun.COM struct  extvtoc	vtoc;		/* table of contents */
590Sstevel@tonic-gate char	*progname;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate extern	struct	badsec_lst *badsl_chain;
620Sstevel@tonic-gate extern	int	badsl_chain_cnt;
630Sstevel@tonic-gate extern	struct	badsec_lst *gbadsl_chain;
640Sstevel@tonic-gate extern	int	gbadsl_chain_cnt;
65*7821SMark.Logan@Sun.COM extern	int	print_altsec(struct extpartition *);
66*7821SMark.Logan@Sun.COM extern	int	updatebadsec(struct extpartition *, int);
67*7821SMark.Logan@Sun.COM extern	void	wr_altsctr(void);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate int		alts_fd;
700Sstevel@tonic-gate 
71362Sbg159949 static void giveusage(void);
72362Sbg159949 static void rd_gbad(FILE *badsecfd);
73362Sbg159949 static void add_gbad(int badsec_entry);
74*7821SMark.Logan@Sun.COM static int try_hw_remap(void);
75*7821SMark.Logan@Sun.COM static int hardware_remap(blkaddr_t);
76362Sbg159949 
77362Sbg159949 int
main(int argc,char * argv[])78362Sbg159949 main(int argc, char *argv[])
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	extern int	optind;
810Sstevel@tonic-gate 	extern char	*optarg;
820Sstevel@tonic-gate 
83*7821SMark.Logan@Sun.COM 	static char	options[] = "Ipa:f:";
840Sstevel@tonic-gate 	char		numbuf[100];
850Sstevel@tonic-gate 	char		*nxtarg;
860Sstevel@tonic-gate 	char		*alts_name;
870Sstevel@tonic-gate 	minor_t 	minor_val;
880Sstevel@tonic-gate 	struct stat 	statbuf;
89*7821SMark.Logan@Sun.COM 	struct extpartition	*part = NULL;
900Sstevel@tonic-gate 	int		alts_slice = -1;
910Sstevel@tonic-gate 	int		l;
920Sstevel@tonic-gate 	int		p;
930Sstevel@tonic-gate 	int		init_flag = 0;
940Sstevel@tonic-gate 	int		print_flag = 0;
950Sstevel@tonic-gate 	int 		c;
960Sstevel@tonic-gate 	int 		i;
970Sstevel@tonic-gate 	FILE		*badsecfd = NULL;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	progname = argv[0];
100*7821SMark.Logan@Sun.COM 	while ((c = getopt(argc, argv, options)) != EOF) {
1010Sstevel@tonic-gate 		switch (c) {
1020Sstevel@tonic-gate 		case 'I':
1030Sstevel@tonic-gate 			init_flag = 1;
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 		case 'p':
1060Sstevel@tonic-gate 			print_flag = 1;
1070Sstevel@tonic-gate 			break;
1080Sstevel@tonic-gate 		case 'a':
1090Sstevel@tonic-gate 			nxtarg = optarg;
110*7821SMark.Logan@Sun.COM 			for (; *nxtarg != '\0'; )
1110Sstevel@tonic-gate 				add_gbad(strtol(nxtarg, &nxtarg, 0));
1120Sstevel@tonic-gate 			break;
1130Sstevel@tonic-gate 		case 'f':
1140Sstevel@tonic-gate 			if ((badsecfd = fopen(optarg, "r")) == NULL) {
115*7821SMark.Logan@Sun.COM 				(void) fprintf(stderr,
116*7821SMark.Logan@Sun.COM 				    "%s: unable to open %s file\n",
117*7821SMark.Logan@Sun.COM 				    progname, optarg);
1180Sstevel@tonic-gate 				exit(1);
1190Sstevel@tonic-gate 			}
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		default:
1220Sstevel@tonic-gate 			giveusage();
1230Sstevel@tonic-gate 			exit(2);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		/* get the last argument -- device stanza */
1280Sstevel@tonic-gate 	if (argc != optind+1) {
129*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "Missing disk device name\n");
1300Sstevel@tonic-gate 		giveusage();
1310Sstevel@tonic-gate 		exit(3);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	devname = argv[optind];
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (stat(devname, &statbuf)) {
136*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: invalid device %s, stat failed\n",
137*7821SMark.Logan@Sun.COM 		    progname, devname);
1380Sstevel@tonic-gate 		giveusage();
1390Sstevel@tonic-gate 		exit(4);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
142*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: device %s is not character"
143*7821SMark.Logan@Sun.COM 		    " special\n", progname, devname);
1440Sstevel@tonic-gate 		giveusage();
1450Sstevel@tonic-gate 		exit(5);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	minor_val = minor(statbuf.st_rdev);
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * NEED A DEFINE FOR THE PHYSICAL BIT (0x10)
1500Sstevel@tonic-gate 	 */
1510Sstevel@tonic-gate 	if ((minor_val & 0x10) == 0) {
152*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: device %s is not a physical"
153*7821SMark.Logan@Sun.COM 		    " slice\n", progname, devname);
1540Sstevel@tonic-gate 		giveusage();
1550Sstevel@tonic-gate 		exit(6);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 	if ((minor_val % V_NUMPAR) != 0) {
158*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: device %s is not a slice 0"
159*7821SMark.Logan@Sun.COM 		    " device\n", progname, devname);
1600Sstevel@tonic-gate 		giveusage();
1610Sstevel@tonic-gate 		exit(7);
1620Sstevel@tonic-gate 	}
163*7821SMark.Logan@Sun.COM 	if ((devfd = open(devname, O_RDWR)) == -1) {
164*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: open of %s failed\n",
165*7821SMark.Logan@Sun.COM 		    progname, devname);
1660Sstevel@tonic-gate 		perror("");
1670Sstevel@tonic-gate 		exit(8);
1680Sstevel@tonic-gate 	}
169*7821SMark.Logan@Sun.COM 	if ((ioctl(devfd, DKIOCGGEOM, &dkg)) == -1) {
170*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: unable to get disk geometry.\n",
171*7821SMark.Logan@Sun.COM 		    progname);
1720Sstevel@tonic-gate 		perror("");
1730Sstevel@tonic-gate 		exit(9);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
176*7821SMark.Logan@Sun.COM 	if (ioctl(devfd, DKIOCGEXTVTOC, &vtoc) == -1) {
177*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: could not get VTOC.\n", progname);
1780Sstevel@tonic-gate 		giveusage();
1790Sstevel@tonic-gate 		exit(14);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if ((vtoc.v_sanity != VTOC_SANE) || (vtoc.v_version != V_VERSION)) {
183*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: invalid VTOC found.\n", progname);
1840Sstevel@tonic-gate 		giveusage();
1850Sstevel@tonic-gate 		exit(15);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	if (badsecfd)
1880Sstevel@tonic-gate 		rd_gbad(badsecfd);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate #ifdef ADDBAD_DEBUG
191*7821SMark.Logan@Sun.COM {
192*7821SMark.Logan@Sun.COM 	struct badsec_lst *blc_p;
1930Sstevel@tonic-gate 	printf("\n main: Total bad sectors found= %d\n", gbadsl_chain_cnt);
194*7821SMark.Logan@Sun.COM 	for (blc_p = gbadsl_chain; blc_p; blc_p = blc_p->bl_nxt) {
195*7821SMark.Logan@Sun.COM 		for (i = 0; i < blc_p->bl_cnt; i++)
1960Sstevel@tonic-gate 			printf(" badsec=%d ", blc_p->bl_sec[i]);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	printf("\n");
199*7821SMark.Logan@Sun.COM }
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate #ifdef PPP
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * If init_flag is set, run to completion.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	if (gbadsl_chain_cnt == 0 && init_flag == 0)
2060Sstevel@tonic-gate 		/*
2070Sstevel@tonic-gate 		 * No defects and not initializing
2080Sstevel@tonic-gate 		 */
209*7821SMark.Logan@Sun.COM 		exit(0);
2100Sstevel@tonic-gate #endif
2110Sstevel@tonic-gate 	if (gbadsl_chain_cnt != 0)
2120Sstevel@tonic-gate 	{
213*7821SMark.Logan@Sun.COM 		if (try_hw_remap() == SUCCESS)
214*7821SMark.Logan@Sun.COM 			exit(0);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * get ALTS slice
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR && alts_slice == -1; i++)
2200Sstevel@tonic-gate 	{
2210Sstevel@tonic-gate 		if (vtoc.v_part[i].p_tag == V_ALTSCTR)
2220Sstevel@tonic-gate 		{
2230Sstevel@tonic-gate 			alts_slice = i;
2240Sstevel@tonic-gate 			part = &vtoc.v_part[i];
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	if (alts_slice == -1)
2280Sstevel@tonic-gate 	{
229*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: No alternates slice.\n", progname);
2300Sstevel@tonic-gate 		exit(16);
2310Sstevel@tonic-gate 	}
232*7821SMark.Logan@Sun.COM 	l = strlen(devname);
233*7821SMark.Logan@Sun.COM 	(void) sprintf(numbuf, "%d", alts_slice);
234*7821SMark.Logan@Sun.COM 	p = strlen(numbuf);
235*7821SMark.Logan@Sun.COM 	alts_name = (char *)malloc(l + p);
236*7821SMark.Logan@Sun.COM 	(void) strcpy(alts_name, devname);
2370Sstevel@tonic-gate 	alts_name[l - 2] = 's';
238*7821SMark.Logan@Sun.COM 	(void) strcpy(&alts_name[l - 1], numbuf);
2390Sstevel@tonic-gate 	alts_name[l + p - 1] = '\0';
240*7821SMark.Logan@Sun.COM 	if ((alts_fd = open(alts_name, O_RDWR)) == -1) {
241*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "%s: open of %s failed\n",
242*7821SMark.Logan@Sun.COM 		    progname, alts_name);
2430Sstevel@tonic-gate 		perror("");
2440Sstevel@tonic-gate 		exit(9);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	if (print_flag)
2470Sstevel@tonic-gate 	{
248*7821SMark.Logan@Sun.COM 		(void) print_altsec(part);
249*7821SMark.Logan@Sun.COM 		exit(0);
2500Sstevel@tonic-gate 	}
251*7821SMark.Logan@Sun.COM 	(void) updatebadsec(part, init_flag);
2520Sstevel@tonic-gate 	wr_altsctr();
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (ioctl(devfd, DKIOCADDBAD, NULL) == -1) {
255*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr,  "Warning: DKIOCADDBAD io control"
256*7821SMark.Logan@Sun.COM 		    " failed. System must be re-booted\n");
257*7821SMark.Logan@Sun.COM 		(void) fprintf(stderr, "for alternate sectors to be usable.\n");
2580Sstevel@tonic-gate 		exit(17);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	sync();
2610Sstevel@tonic-gate 
262*7821SMark.Logan@Sun.COM 	(void) fclose(badsecfd);
263*7821SMark.Logan@Sun.COM 	(void) close(alts_fd);
264*7821SMark.Logan@Sun.COM 	(void) close(devfd);
265*7821SMark.Logan@Sun.COM 	return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * Giveusage ()
2700Sstevel@tonic-gate  * Give a (not so) concise message on how to use this program.
2710Sstevel@tonic-gate  */
272*7821SMark.Logan@Sun.COM static void
giveusage(void)273*7821SMark.Logan@Sun.COM giveusage(void)
2740Sstevel@tonic-gate {
275*7821SMark.Logan@Sun.COM 	(void) fprintf(stderr, "%s [-p] [-a sector] [-f filename]"
276*7821SMark.Logan@Sun.COM 	    " raw-device\n", progname);
277*7821SMark.Logan@Sun.COM 	(void) fprintf(stderr, "	p - Print existing bad block map\n");
278*7821SMark.Logan@Sun.COM 	(void) fprintf(stderr, "	a - Add the given sectors to the"
279*7821SMark.Logan@Sun.COM 	    " bad block list\n");
280*7821SMark.Logan@Sun.COM 	(void) fprintf(stderr, "	f - Add the sectors from <filename>"
281*7821SMark.Logan@Sun.COM 	    " to the bad block list\n");
2820Sstevel@tonic-gate 	if (devfd)
283*7821SMark.Logan@Sun.COM 		(void) close(devfd);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
288*7821SMark.Logan@Sun.COM  *	read in the additional growing bad sectors
2890Sstevel@tonic-gate  */
290362Sbg159949 static void
rd_gbad(FILE * badsecfd)291362Sbg159949 rd_gbad(FILE *badsecfd)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	int	badsec_entry;
2940Sstevel@tonic-gate 	int	status;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	status = fscanf(badsecfd, "%d", &badsec_entry);
297*7821SMark.Logan@Sun.COM 	while (status != EOF) {
2980Sstevel@tonic-gate 		add_gbad(badsec_entry);
2990Sstevel@tonic-gate 		status = fscanf(badsecfd, "%d", &badsec_entry);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
303362Sbg159949 static void
add_gbad(int badsec_entry)304362Sbg159949 add_gbad(int badsec_entry)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	struct badsec_lst *blc_p;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (!gbadsl_chain) {
3090Sstevel@tonic-gate 		blc_p = (struct badsec_lst *)malloc(BADSLSZ);
3100Sstevel@tonic-gate 		if (!blc_p) {
311*7821SMark.Logan@Sun.COM 			(void) fprintf(stderr, "Unable to allocate memory"
312*7821SMark.Logan@Sun.COM 			    " for additional bad sectors\n");
3130Sstevel@tonic-gate 			exit(18);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 		gbadsl_chain = blc_p;
3160Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
3170Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
3200Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
321*7821SMark.Logan@Sun.COM 
3220Sstevel@tonic-gate 	if (blc_p->bl_cnt == MAXBLENT) {
3230Sstevel@tonic-gate 		blc_p->bl_nxt = (struct badsec_lst *)malloc(BADSLSZ);
3240Sstevel@tonic-gate 		if (!blc_p->bl_nxt) {
325*7821SMark.Logan@Sun.COM 			(void) fprintf(stderr, "Unable to allocate memory"
326*7821SMark.Logan@Sun.COM 			    " for additional bad sectors\n");
3270Sstevel@tonic-gate 			exit(19);
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
3300Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
3310Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	blc_p->bl_sec[blc_p->bl_cnt++] = badsec_entry;
3340Sstevel@tonic-gate 	gbadsl_chain_cnt++;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * Map a block using hardware (SCSI) techniques.
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate /*ARGSUSED*/
341*7821SMark.Logan@Sun.COM static int
hardware_remap(bn)342*7821SMark.Logan@Sun.COM hardware_remap(bn)
343*7821SMark.Logan@Sun.COM blkaddr_t	bn;
3440Sstevel@tonic-gate {
345*7821SMark.Logan@Sun.COM 	uint_t		byte_swap_32(uint_t);
346*7821SMark.Logan@Sun.COM 	ushort_t	byte_swap_16(ushort_t);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	struct uscsi_cmd		ucmd;
3490Sstevel@tonic-gate 	union scsi_cdb			cdb;
3500Sstevel@tonic-gate 	struct scsi_reassign_blk	defect_list;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Build and execute the uscsi ioctl
3540Sstevel@tonic-gate 	 */
3550Sstevel@tonic-gate 	(void) memset((char *)&ucmd, 0, sizeof (ucmd));
3560Sstevel@tonic-gate 	(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
3570Sstevel@tonic-gate 	(void) memset((char *)&defect_list, 0,
3580Sstevel@tonic-gate 		sizeof (struct scsi_reassign_blk));
3590Sstevel@tonic-gate 	cdb.scc_cmd = SCMD_REASSIGN_BLOCK;
360*7821SMark.Logan@Sun.COM 	ucmd.uscsi_cdb = (caddr_t)&cdb;
3610Sstevel@tonic-gate 	ucmd.uscsi_cdblen = CDB_GROUP0;
362*7821SMark.Logan@Sun.COM 	ucmd.uscsi_bufaddr = (caddr_t)&defect_list;
3630Sstevel@tonic-gate 	ucmd.uscsi_buflen = sizeof (struct scsi_reassign_blk);
364*7821SMark.Logan@Sun.COM 	defect_list.length = byte_swap_16(sizeof (defect_list.defect));
365*7821SMark.Logan@Sun.COM 	defect_list.defect = byte_swap_32(bn);
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * Set function flags for driver.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_DIAGNOSE | USCSI_SILENT;
3700Sstevel@tonic-gate 	ucmd.uscsi_timeout = 30;		/* 30 seconds */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * Execute the ioctl
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	if (ioctl(devfd, USCSICMD, &ucmd) == -1)
3760Sstevel@tonic-gate 	{
3770Sstevel@tonic-gate 		if (errno != ENOTTY)
3780Sstevel@tonic-gate 		{
379*7821SMark.Logan@Sun.COM 			perror("SCSI hardware re-assign failed");
3800Sstevel@tonic-gate 			/*
3810Sstevel@tonic-gate 			 * It looks like a failure but by returning success
3820Sstevel@tonic-gate 			 * the upper layer will not try to do
3830Sstevel@tonic-gate 			 * software remapping.
3840Sstevel@tonic-gate 			 */
3850Sstevel@tonic-gate 			return (SUCCESS);
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 		return (FAILURE);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	return (SUCCESS);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
392*7821SMark.Logan@Sun.COM uint_t
byte_swap_32(uint_t nav)393*7821SMark.Logan@Sun.COM byte_swap_32(uint_t nav)
3940Sstevel@tonic-gate {
395*7821SMark.Logan@Sun.COM 	uint_t	rc;
3960Sstevel@tonic-gate 	rc = ((nav & 0xff000000) >> 24) | ((nav & 0x00ff0000) >> 8) |
397*7821SMark.Logan@Sun.COM 	    ((nav & 0x0000ff00) << 8)  | ((nav & 0x000000ff) << 24);
3980Sstevel@tonic-gate 	return (rc);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
401*7821SMark.Logan@Sun.COM ushort_t
byte_swap_16(ushort_t niv)402*7821SMark.Logan@Sun.COM byte_swap_16(ushort_t niv)
4030Sstevel@tonic-gate {
404*7821SMark.Logan@Sun.COM 	ushort_t	rc;
405*7821SMark.Logan@Sun.COM 	rc = (ushort_t)((int)(niv & 0xff00) >> 8) | ((niv & 0x00ff) << 8);
4060Sstevel@tonic-gate 	return (rc);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
409*7821SMark.Logan@Sun.COM static int
try_hw_remap()410*7821SMark.Logan@Sun.COM try_hw_remap()
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	struct badsec_lst *blc_p;
4130Sstevel@tonic-gate 	int	i;
4140Sstevel@tonic-gate 
415*7821SMark.Logan@Sun.COM 	for (blc_p = gbadsl_chain; blc_p != 0; blc_p = blc_p->bl_nxt) {
4160Sstevel@tonic-gate 		for (i = 0; i < blc_p->bl_cnt; i++)
417*7821SMark.Logan@Sun.COM 		if (hardware_remap(blc_p->bl_sec[i]) == FAILURE)
4180Sstevel@tonic-gate 			return (FAILURE);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	return (SUCCESS);
4210Sstevel@tonic-gate }
422