xref: /titanic_50/usr/src/cmd/addbadsec/addbadsec.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
28*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
29*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * Copyrighted as an unpublished work.
33*7c478bd9Sstevel@tonic-gate  * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
34*7c478bd9Sstevel@tonic-gate  * All rights reserved.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <stdio.h>
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <memory.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
48*7c478bd9Sstevel@tonic-gate #include <errno.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/scsi/generic/commands.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/commands.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
52*7c478bd9Sstevel@tonic-gate #include "badsec.h"
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate char    *devname;		/* name of device */
55*7c478bd9Sstevel@tonic-gate int	devfd;			/* device file descriptor */
56*7c478bd9Sstevel@tonic-gate struct  dk_geom      	dkg;	/* geometry */
57*7c478bd9Sstevel@tonic-gate struct  vtoc	vtoc;		/* table of contents */
58*7c478bd9Sstevel@tonic-gate char	*progname;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate extern	struct	badsec_lst *badsl_chain;
61*7c478bd9Sstevel@tonic-gate extern	int	badsl_chain_cnt;
62*7c478bd9Sstevel@tonic-gate extern	struct	badsec_lst *gbadsl_chain;
63*7c478bd9Sstevel@tonic-gate extern	int	gbadsl_chain_cnt;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate int		alts_fd;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate void
68*7c478bd9Sstevel@tonic-gate main(argc, argv)
69*7c478bd9Sstevel@tonic-gate int	argc;
70*7c478bd9Sstevel@tonic-gate char	*argv[];
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	extern int	optind;
73*7c478bd9Sstevel@tonic-gate 	extern char	*optarg;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	static char     options[] = "Ipa:f:";
76*7c478bd9Sstevel@tonic-gate 	char		numbuf[100];
77*7c478bd9Sstevel@tonic-gate 	char		*nxtarg;
78*7c478bd9Sstevel@tonic-gate 	char		*alts_name;
79*7c478bd9Sstevel@tonic-gate 	minor_t 	minor_val;
80*7c478bd9Sstevel@tonic-gate 	struct stat 	statbuf;
81*7c478bd9Sstevel@tonic-gate 	struct partition	*part = NULL;
82*7c478bd9Sstevel@tonic-gate 	int		alts_slice = -1;
83*7c478bd9Sstevel@tonic-gate 	int		l;
84*7c478bd9Sstevel@tonic-gate 	int		p;
85*7c478bd9Sstevel@tonic-gate 	int		init_flag = 0;
86*7c478bd9Sstevel@tonic-gate 	int		print_flag = 0;
87*7c478bd9Sstevel@tonic-gate 	int 		c;
88*7c478bd9Sstevel@tonic-gate 	int 		i;
89*7c478bd9Sstevel@tonic-gate 	FILE		*badsecfd = NULL;
90*7c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	progname = argv[0];
93*7c478bd9Sstevel@tonic-gate 	while ( (c=getopt(argc, argv, options)) != EOF ) {
94*7c478bd9Sstevel@tonic-gate 		switch (c) {
95*7c478bd9Sstevel@tonic-gate 		case 'I':
96*7c478bd9Sstevel@tonic-gate 			init_flag = 1;
97*7c478bd9Sstevel@tonic-gate 			break;
98*7c478bd9Sstevel@tonic-gate 		case 'p':
99*7c478bd9Sstevel@tonic-gate 			print_flag = 1;
100*7c478bd9Sstevel@tonic-gate 			break;
101*7c478bd9Sstevel@tonic-gate 		case 'a':
102*7c478bd9Sstevel@tonic-gate 			nxtarg = optarg;
103*7c478bd9Sstevel@tonic-gate 			for (;*nxtarg != '\0';)
104*7c478bd9Sstevel@tonic-gate 				add_gbad(strtol(nxtarg, &nxtarg, 0));
105*7c478bd9Sstevel@tonic-gate 			break;
106*7c478bd9Sstevel@tonic-gate 		case 'f':
107*7c478bd9Sstevel@tonic-gate 			if ((badsecfd = fopen(optarg, "r")) == NULL) {
108*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: unable to open %s file\n", progname, optarg);
109*7c478bd9Sstevel@tonic-gate 				exit(1);
110*7c478bd9Sstevel@tonic-gate 			}
111*7c478bd9Sstevel@tonic-gate 			break;
112*7c478bd9Sstevel@tonic-gate 		default:
113*7c478bd9Sstevel@tonic-gate 			giveusage();
114*7c478bd9Sstevel@tonic-gate 			exit(2);
115*7c478bd9Sstevel@tonic-gate 		}
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 		/* get the last argument -- device stanza */
119*7c478bd9Sstevel@tonic-gate 	if (argc != optind+1) {
120*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Missing disk device name\n");
121*7c478bd9Sstevel@tonic-gate 		giveusage();
122*7c478bd9Sstevel@tonic-gate 		exit(3);
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	devname = argv[optind];
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (stat(devname, &statbuf)) {
127*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: invalid device %s, stat failed\n", progname, devname);
128*7c478bd9Sstevel@tonic-gate 		giveusage();
129*7c478bd9Sstevel@tonic-gate 		exit(4);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
132*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not character special\n", progname, devname);
133*7c478bd9Sstevel@tonic-gate 		giveusage();
134*7c478bd9Sstevel@tonic-gate 		exit(5);
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 	minor_val = minor(statbuf.st_rdev);
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * NEED A DEFINE FOR THE PHYSICAL BIT (0x10)
139*7c478bd9Sstevel@tonic-gate 	 */
140*7c478bd9Sstevel@tonic-gate 	if ((minor_val & 0x10) == 0) {
141*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not a physical slice\n", progname, devname);
142*7c478bd9Sstevel@tonic-gate 		giveusage();
143*7c478bd9Sstevel@tonic-gate 		exit(6);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	if ((minor_val % V_NUMPAR) != 0) {
146*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not a slice 0 device\n", progname, devname);
147*7c478bd9Sstevel@tonic-gate 		giveusage();
148*7c478bd9Sstevel@tonic-gate 		exit(7);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	if ((devfd=open(devname, O_RDWR)) == -1) {
151*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: open of %s failed\n", progname ,devname);
152*7c478bd9Sstevel@tonic-gate 		perror("");
153*7c478bd9Sstevel@tonic-gate 		exit(8);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 	if ((ioctl (devfd, DKIOCGGEOM, &dkg)) == -1) {
156*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: unable to get disk geometry.\n", progname);
157*7c478bd9Sstevel@tonic-gate 		perror("");
158*7c478bd9Sstevel@tonic-gate 		exit(9);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, DKIOCGVTOC, &vtoc) == -1)
162*7c478bd9Sstevel@tonic-gate 	{
163*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: could not get VTOC.\n", progname);
164*7c478bd9Sstevel@tonic-gate 		giveusage();
165*7c478bd9Sstevel@tonic-gate 		exit(14);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if ((vtoc.v_sanity != VTOC_SANE) || (vtoc.v_version != V_VERSION)) {
169*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: invalid VTOC found.\n", progname);
170*7c478bd9Sstevel@tonic-gate 		giveusage();
171*7c478bd9Sstevel@tonic-gate 		exit(15);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	if (badsecfd)
174*7c478bd9Sstevel@tonic-gate 		rd_gbad(badsecfd);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate #ifdef ADDBAD_DEBUG
177*7c478bd9Sstevel@tonic-gate 	printf("\n main: Total bad sectors found= %d\n", gbadsl_chain_cnt);
178*7c478bd9Sstevel@tonic-gate 	for (blc_p=gbadsl_chain; blc_p; blc_p=blc_p->bl_nxt) {
179*7c478bd9Sstevel@tonic-gate 		for (i=0; i<blc_p->bl_cnt; i++)
180*7c478bd9Sstevel@tonic-gate 			printf(" badsec=%d ", blc_p->bl_sec[i]);
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 	printf("\n");
183*7c478bd9Sstevel@tonic-gate #endif
184*7c478bd9Sstevel@tonic-gate #ifdef PPP
185*7c478bd9Sstevel@tonic-gate 	/*
186*7c478bd9Sstevel@tonic-gate 	 * If init_flag is set, run to completion.
187*7c478bd9Sstevel@tonic-gate 	 */
188*7c478bd9Sstevel@tonic-gate 	if (gbadsl_chain_cnt == 0 && init_flag == 0)
189*7c478bd9Sstevel@tonic-gate 		/*
190*7c478bd9Sstevel@tonic-gate 		 * No defects and not initializing
191*7c478bd9Sstevel@tonic-gate 		 */
192*7c478bd9Sstevel@tonic-gate 		exit (0);
193*7c478bd9Sstevel@tonic-gate #endif
194*7c478bd9Sstevel@tonic-gate 	if (gbadsl_chain_cnt != 0)
195*7c478bd9Sstevel@tonic-gate 	{
196*7c478bd9Sstevel@tonic-gate 		if (try_hw_remap () == SUCCESS)
197*7c478bd9Sstevel@tonic-gate 			exit (0);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	/*
200*7c478bd9Sstevel@tonic-gate 	 * get ALTS slice
201*7c478bd9Sstevel@tonic-gate 	 */
202*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR && alts_slice == -1; i++)
203*7c478bd9Sstevel@tonic-gate 	{
204*7c478bd9Sstevel@tonic-gate 		if (vtoc.v_part[i].p_tag == V_ALTSCTR)
205*7c478bd9Sstevel@tonic-gate 		{
206*7c478bd9Sstevel@tonic-gate 			alts_slice = i;
207*7c478bd9Sstevel@tonic-gate 			part = &vtoc.v_part[i];
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	if (alts_slice == -1)
211*7c478bd9Sstevel@tonic-gate 	{
212*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: No alternates slice.\n", progname);
213*7c478bd9Sstevel@tonic-gate 		exit(16);
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 	l = strlen (devname);
216*7c478bd9Sstevel@tonic-gate 	sprintf (numbuf, "%d", alts_slice);
217*7c478bd9Sstevel@tonic-gate 	p = strlen (numbuf);
218*7c478bd9Sstevel@tonic-gate 	alts_name = (char *)malloc (l + p);
219*7c478bd9Sstevel@tonic-gate 	strcpy (alts_name, devname);
220*7c478bd9Sstevel@tonic-gate 	alts_name[l - 2] = 's';
221*7c478bd9Sstevel@tonic-gate 	strcpy (&alts_name[l - 1], numbuf);
222*7c478bd9Sstevel@tonic-gate 	alts_name[l + p - 1] = '\0';
223*7c478bd9Sstevel@tonic-gate 	if ((alts_fd=open(alts_name, O_RDWR)) == -1) {
224*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: open of %s failed\n", progname ,alts_name);
225*7c478bd9Sstevel@tonic-gate 		perror("");
226*7c478bd9Sstevel@tonic-gate 		exit(9);
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 	if (print_flag)
229*7c478bd9Sstevel@tonic-gate 	{
230*7c478bd9Sstevel@tonic-gate 		print_altsec (part);
231*7c478bd9Sstevel@tonic-gate 		exit (0);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	updatebadsec(part, init_flag);
234*7c478bd9Sstevel@tonic-gate 	wr_altsctr();
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, DKIOCADDBAD, NULL) == -1) {
237*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,  "Warning: DKIOCADDBAD io control failed. System must be re-booted\n");
238*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "for alternate sectors to be usable.\n");
239*7c478bd9Sstevel@tonic-gate 		exit(17);
240*7c478bd9Sstevel@tonic-gate 	}
241*7c478bd9Sstevel@tonic-gate 	sync();
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	fclose(badsecfd);
244*7c478bd9Sstevel@tonic-gate 	close (alts_fd);
245*7c478bd9Sstevel@tonic-gate 	close (devfd);
246*7c478bd9Sstevel@tonic-gate 	exit(0);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * Giveusage ()
251*7c478bd9Sstevel@tonic-gate  * Give a (not so) concise message on how to use this program.
252*7c478bd9Sstevel@tonic-gate  */
253*7c478bd9Sstevel@tonic-gate giveusage()
254*7c478bd9Sstevel@tonic-gate {
255*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s [-p] [-a sector] [-f filename] raw-device\n", progname);
256*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	p - Print existing bad block map\n");
257*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	a - Add the given sectors to the bad block list\n");
258*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	f - Add the sectors from <filename> to the bad block list\n");
259*7c478bd9Sstevel@tonic-gate 	if (devfd)
260*7c478bd9Sstevel@tonic-gate 		close(devfd);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate /*
265*7c478bd9Sstevel@tonic-gate  *	read in the additional growing bad sectors
266*7c478bd9Sstevel@tonic-gate  */
267*7c478bd9Sstevel@tonic-gate rd_gbad(badsecfd)
268*7c478bd9Sstevel@tonic-gate FILE	*badsecfd;
269*7c478bd9Sstevel@tonic-gate {
270*7c478bd9Sstevel@tonic-gate 	int	badsec_entry;
271*7c478bd9Sstevel@tonic-gate 	int	status;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	status = fscanf(badsecfd, "%d", &badsec_entry);
274*7c478bd9Sstevel@tonic-gate 	while (status!=EOF) {
275*7c478bd9Sstevel@tonic-gate 		add_gbad(badsec_entry);
276*7c478bd9Sstevel@tonic-gate 		status = fscanf(badsecfd, "%d", &badsec_entry);
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate add_gbad(badsec_entry)
281*7c478bd9Sstevel@tonic-gate int	badsec_entry;
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	if (!gbadsl_chain) {
286*7c478bd9Sstevel@tonic-gate 		blc_p = (struct badsec_lst *)malloc(BADSLSZ);
287*7c478bd9Sstevel@tonic-gate 		if (!blc_p) {
288*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unable to allocate memory for additional bad sectors\n");
289*7c478bd9Sstevel@tonic-gate 			exit(18);
290*7c478bd9Sstevel@tonic-gate 		}
291*7c478bd9Sstevel@tonic-gate 		gbadsl_chain = blc_p;
292*7c478bd9Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
293*7c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
296*7c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	if (blc_p->bl_cnt == MAXBLENT) {
299*7c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = (struct badsec_lst *)malloc(BADSLSZ);
300*7c478bd9Sstevel@tonic-gate 		if (!blc_p->bl_nxt) {
301*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unable to allocate memory for additional bad sectors\n");
302*7c478bd9Sstevel@tonic-gate 			exit(19);
303*7c478bd9Sstevel@tonic-gate 		}
304*7c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
305*7c478bd9Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
306*7c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 	blc_p->bl_sec[blc_p->bl_cnt++] = badsec_entry;
309*7c478bd9Sstevel@tonic-gate 	gbadsl_chain_cnt++;
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate /*
313*7c478bd9Sstevel@tonic-gate  * Map a block using hardware (SCSI) techniques.
314*7c478bd9Sstevel@tonic-gate  */
315*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
316*7c478bd9Sstevel@tonic-gate int
317*7c478bd9Sstevel@tonic-gate hardware_remap (bn)
318*7c478bd9Sstevel@tonic-gate int	bn;
319*7c478bd9Sstevel@tonic-gate {
320*7c478bd9Sstevel@tonic-gate 	u_int	byte_swap_32 (u_int);
321*7c478bd9Sstevel@tonic-gate 	u_short	byte_swap_16 (u_short);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	struct uscsi_cmd		ucmd;
324*7c478bd9Sstevel@tonic-gate 	union scsi_cdb			cdb;
325*7c478bd9Sstevel@tonic-gate 	struct scsi_reassign_blk	defect_list;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	/*
328*7c478bd9Sstevel@tonic-gate 	 * Build and execute the uscsi ioctl
329*7c478bd9Sstevel@tonic-gate 	 */
330*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&ucmd, 0, sizeof (ucmd));
331*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
332*7c478bd9Sstevel@tonic-gate 	(void) memset((char *)&defect_list, 0,
333*7c478bd9Sstevel@tonic-gate 		sizeof (struct scsi_reassign_blk));
334*7c478bd9Sstevel@tonic-gate 	cdb.scc_cmd = SCMD_REASSIGN_BLOCK;
335*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_cdb = (caddr_t) &cdb;
336*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_cdblen = CDB_GROUP0;
337*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_bufaddr = (caddr_t) &defect_list;
338*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_buflen = sizeof (struct scsi_reassign_blk);
339*7c478bd9Sstevel@tonic-gate 	defect_list.length = byte_swap_16 (sizeof (defect_list.defect));
340*7c478bd9Sstevel@tonic-gate 	defect_list.defect = byte_swap_32 (bn);
341*7c478bd9Sstevel@tonic-gate 	/*
342*7c478bd9Sstevel@tonic-gate 	printf ("length - %x %x\n", sizeof (defect_list.defect), defect_list.length);
343*7c478bd9Sstevel@tonic-gate 	printf ("defect - %x %x\n", bn, defect_list.defect);
344*7c478bd9Sstevel@tonic-gate 	*/
345*7c478bd9Sstevel@tonic-gate 	/*
346*7c478bd9Sstevel@tonic-gate 	 * Set function flags for driver.
347*7c478bd9Sstevel@tonic-gate 	 */
348*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_DIAGNOSE | USCSI_SILENT;
349*7c478bd9Sstevel@tonic-gate 	ucmd.uscsi_timeout = 30;		/* 30 seconds */
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	/*
352*7c478bd9Sstevel@tonic-gate 	 * Execute the ioctl
353*7c478bd9Sstevel@tonic-gate 	 */
354*7c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, USCSICMD, &ucmd) == -1)
355*7c478bd9Sstevel@tonic-gate 	{
356*7c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY)
357*7c478bd9Sstevel@tonic-gate 		{
358*7c478bd9Sstevel@tonic-gate 			perror ("SCSI hardware re-assign failed");
359*7c478bd9Sstevel@tonic-gate 			/*
360*7c478bd9Sstevel@tonic-gate 			 * It looks like a failure but by returning success
361*7c478bd9Sstevel@tonic-gate 			 * the upper layer will not try to do
362*7c478bd9Sstevel@tonic-gate 			 * software remapping.
363*7c478bd9Sstevel@tonic-gate 			 */
364*7c478bd9Sstevel@tonic-gate 			return (SUCCESS);
365*7c478bd9Sstevel@tonic-gate 		}
366*7c478bd9Sstevel@tonic-gate 		return (FAILURE);
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate u_int
372*7c478bd9Sstevel@tonic-gate byte_swap_32 (u_int nav)
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	u_int	rc;
375*7c478bd9Sstevel@tonic-gate 	rc = ((nav & 0xff000000) >> 24) | ((nav & 0x00ff0000) >> 8) |
376*7c478bd9Sstevel@tonic-gate 	     ((nav & 0x0000ff00) << 8)  | ((nav & 0x000000ff) << 24);
377*7c478bd9Sstevel@tonic-gate 	return (rc);
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate u_short
381*7c478bd9Sstevel@tonic-gate byte_swap_16 (u_short niv)
382*7c478bd9Sstevel@tonic-gate {
383*7c478bd9Sstevel@tonic-gate 	u_short	rc;
384*7c478bd9Sstevel@tonic-gate 	rc = (u_short)((int)(niv & 0xff00) >> 8) | ((niv & 0x00ff) << 8);
385*7c478bd9Sstevel@tonic-gate 	return (rc);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate try_hw_remap ()
389*7c478bd9Sstevel@tonic-gate {
390*7c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
391*7c478bd9Sstevel@tonic-gate 	int	i;
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p != 0; blc_p = blc_p->bl_nxt)
394*7c478bd9Sstevel@tonic-gate 	{
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < blc_p->bl_cnt; i++)
397*7c478bd9Sstevel@tonic-gate 		if (hardware_remap (blc_p->bl_sec[i]) == FAILURE)
398*7c478bd9Sstevel@tonic-gate 			return (FAILURE);
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
401*7c478bd9Sstevel@tonic-gate }
402