xref: /titanic_50/usr/src/cmd/addbadsec/addbadsec.c (revision 052b6e8a13e1fb4be51ba482db6bb2e9833fa717)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*052b6e8aSbg159949  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Copyrighted as an unpublished work.
337c478bd9Sstevel@tonic-gate  * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
347c478bd9Sstevel@tonic-gate  * All rights reserved.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <memory.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
467c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
477c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/scsi/generic/commands.h>
507c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/commands.h>
517c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
527c478bd9Sstevel@tonic-gate #include "badsec.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate char    *devname;		/* name of device */
557c478bd9Sstevel@tonic-gate int	devfd;			/* device file descriptor */
567c478bd9Sstevel@tonic-gate struct  dk_geom      	dkg;	/* geometry */
577c478bd9Sstevel@tonic-gate struct  vtoc	vtoc;		/* table of contents */
587c478bd9Sstevel@tonic-gate char	*progname;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate extern	struct	badsec_lst *badsl_chain;
617c478bd9Sstevel@tonic-gate extern	int	badsl_chain_cnt;
627c478bd9Sstevel@tonic-gate extern	struct	badsec_lst *gbadsl_chain;
637c478bd9Sstevel@tonic-gate extern	int	gbadsl_chain_cnt;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int		alts_fd;
667c478bd9Sstevel@tonic-gate 
67*052b6e8aSbg159949 static void giveusage(void);
68*052b6e8aSbg159949 static void rd_gbad(FILE *badsecfd);
69*052b6e8aSbg159949 static void add_gbad(int badsec_entry);
70*052b6e8aSbg159949 
71*052b6e8aSbg159949 int
72*052b6e8aSbg159949 main(int argc, char *argv[])
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	extern int	optind;
757c478bd9Sstevel@tonic-gate 	extern char	*optarg;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	static char     options[] = "Ipa:f:";
787c478bd9Sstevel@tonic-gate 	char		numbuf[100];
797c478bd9Sstevel@tonic-gate 	char		*nxtarg;
807c478bd9Sstevel@tonic-gate 	char		*alts_name;
817c478bd9Sstevel@tonic-gate 	minor_t 	minor_val;
827c478bd9Sstevel@tonic-gate 	struct stat 	statbuf;
837c478bd9Sstevel@tonic-gate 	struct partition	*part = NULL;
847c478bd9Sstevel@tonic-gate 	int		alts_slice = -1;
857c478bd9Sstevel@tonic-gate 	int		l;
867c478bd9Sstevel@tonic-gate 	int		p;
877c478bd9Sstevel@tonic-gate 	int		init_flag = 0;
887c478bd9Sstevel@tonic-gate 	int		print_flag = 0;
897c478bd9Sstevel@tonic-gate 	int 		c;
907c478bd9Sstevel@tonic-gate 	int 		i;
917c478bd9Sstevel@tonic-gate 	FILE		*badsecfd = NULL;
927c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	progname = argv[0];
957c478bd9Sstevel@tonic-gate 	while ( (c=getopt(argc, argv, options)) != EOF ) {
967c478bd9Sstevel@tonic-gate 		switch (c) {
977c478bd9Sstevel@tonic-gate 		case 'I':
987c478bd9Sstevel@tonic-gate 			init_flag = 1;
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		case 'p':
1017c478bd9Sstevel@tonic-gate 			print_flag = 1;
1027c478bd9Sstevel@tonic-gate 			break;
1037c478bd9Sstevel@tonic-gate 		case 'a':
1047c478bd9Sstevel@tonic-gate 			nxtarg = optarg;
1057c478bd9Sstevel@tonic-gate 			for (;*nxtarg != '\0';)
1067c478bd9Sstevel@tonic-gate 				add_gbad(strtol(nxtarg, &nxtarg, 0));
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case 'f':
1097c478bd9Sstevel@tonic-gate 			if ((badsecfd = fopen(optarg, "r")) == NULL) {
1107c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: unable to open %s file\n", progname, optarg);
1117c478bd9Sstevel@tonic-gate 				exit(1);
1127c478bd9Sstevel@tonic-gate 			}
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		default:
1157c478bd9Sstevel@tonic-gate 			giveusage();
1167c478bd9Sstevel@tonic-gate 			exit(2);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		/* get the last argument -- device stanza */
1217c478bd9Sstevel@tonic-gate 	if (argc != optind+1) {
1227c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Missing disk device name\n");
1237c478bd9Sstevel@tonic-gate 		giveusage();
1247c478bd9Sstevel@tonic-gate 		exit(3);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	devname = argv[optind];
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (stat(devname, &statbuf)) {
1297c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: invalid device %s, stat failed\n", progname, devname);
1307c478bd9Sstevel@tonic-gate 		giveusage();
1317c478bd9Sstevel@tonic-gate 		exit(4);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
1347c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not character special\n", progname, devname);
1357c478bd9Sstevel@tonic-gate 		giveusage();
1367c478bd9Sstevel@tonic-gate 		exit(5);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	minor_val = minor(statbuf.st_rdev);
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * NEED A DEFINE FOR THE PHYSICAL BIT (0x10)
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	if ((minor_val & 0x10) == 0) {
1437c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not a physical slice\n", progname, devname);
1447c478bd9Sstevel@tonic-gate 		giveusage();
1457c478bd9Sstevel@tonic-gate 		exit(6);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	if ((minor_val % V_NUMPAR) != 0) {
1487c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: device %s is not a slice 0 device\n", progname, devname);
1497c478bd9Sstevel@tonic-gate 		giveusage();
1507c478bd9Sstevel@tonic-gate 		exit(7);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	if ((devfd=open(devname, O_RDWR)) == -1) {
1537c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: open of %s failed\n", progname ,devname);
1547c478bd9Sstevel@tonic-gate 		perror("");
1557c478bd9Sstevel@tonic-gate 		exit(8);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	if ((ioctl (devfd, DKIOCGGEOM, &dkg)) == -1) {
1587c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: unable to get disk geometry.\n", progname);
1597c478bd9Sstevel@tonic-gate 		perror("");
1607c478bd9Sstevel@tonic-gate 		exit(9);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, DKIOCGVTOC, &vtoc) == -1)
1647c478bd9Sstevel@tonic-gate 	{
1657c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: could not get VTOC.\n", progname);
1667c478bd9Sstevel@tonic-gate 		giveusage();
1677c478bd9Sstevel@tonic-gate 		exit(14);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if ((vtoc.v_sanity != VTOC_SANE) || (vtoc.v_version != V_VERSION)) {
1717c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: invalid VTOC found.\n", progname);
1727c478bd9Sstevel@tonic-gate 		giveusage();
1737c478bd9Sstevel@tonic-gate 		exit(15);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (badsecfd)
1767c478bd9Sstevel@tonic-gate 		rd_gbad(badsecfd);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #ifdef ADDBAD_DEBUG
1797c478bd9Sstevel@tonic-gate 	printf("\n main: Total bad sectors found= %d\n", gbadsl_chain_cnt);
1807c478bd9Sstevel@tonic-gate 	for (blc_p=gbadsl_chain; blc_p; blc_p=blc_p->bl_nxt) {
1817c478bd9Sstevel@tonic-gate 		for (i=0; i<blc_p->bl_cnt; i++)
1827c478bd9Sstevel@tonic-gate 			printf(" badsec=%d ", blc_p->bl_sec[i]);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	printf("\n");
1857c478bd9Sstevel@tonic-gate #endif
1867c478bd9Sstevel@tonic-gate #ifdef PPP
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * If init_flag is set, run to completion.
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	if (gbadsl_chain_cnt == 0 && init_flag == 0)
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * No defects and not initializing
1937c478bd9Sstevel@tonic-gate 		 */
1947c478bd9Sstevel@tonic-gate 		exit (0);
1957c478bd9Sstevel@tonic-gate #endif
1967c478bd9Sstevel@tonic-gate 	if (gbadsl_chain_cnt != 0)
1977c478bd9Sstevel@tonic-gate 	{
1987c478bd9Sstevel@tonic-gate 		if (try_hw_remap () == SUCCESS)
1997c478bd9Sstevel@tonic-gate 			exit (0);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * get ALTS slice
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR && alts_slice == -1; i++)
2057c478bd9Sstevel@tonic-gate 	{
2067c478bd9Sstevel@tonic-gate 		if (vtoc.v_part[i].p_tag == V_ALTSCTR)
2077c478bd9Sstevel@tonic-gate 		{
2087c478bd9Sstevel@tonic-gate 			alts_slice = i;
2097c478bd9Sstevel@tonic-gate 			part = &vtoc.v_part[i];
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	if (alts_slice == -1)
2137c478bd9Sstevel@tonic-gate 	{
2147c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: No alternates slice.\n", progname);
2157c478bd9Sstevel@tonic-gate 		exit(16);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	l = strlen (devname);
2187c478bd9Sstevel@tonic-gate 	sprintf (numbuf, "%d", alts_slice);
2197c478bd9Sstevel@tonic-gate 	p = strlen (numbuf);
2207c478bd9Sstevel@tonic-gate 	alts_name = (char *)malloc (l + p);
2217c478bd9Sstevel@tonic-gate 	strcpy (alts_name, devname);
2227c478bd9Sstevel@tonic-gate 	alts_name[l - 2] = 's';
2237c478bd9Sstevel@tonic-gate 	strcpy (&alts_name[l - 1], numbuf);
2247c478bd9Sstevel@tonic-gate 	alts_name[l + p - 1] = '\0';
2257c478bd9Sstevel@tonic-gate 	if ((alts_fd=open(alts_name, O_RDWR)) == -1) {
2267c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: open of %s failed\n", progname ,alts_name);
2277c478bd9Sstevel@tonic-gate 		perror("");
2287c478bd9Sstevel@tonic-gate 		exit(9);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	if (print_flag)
2317c478bd9Sstevel@tonic-gate 	{
2327c478bd9Sstevel@tonic-gate 		print_altsec (part);
2337c478bd9Sstevel@tonic-gate 		exit (0);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	updatebadsec(part, init_flag);
2367c478bd9Sstevel@tonic-gate 	wr_altsctr();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, DKIOCADDBAD, NULL) == -1) {
2397c478bd9Sstevel@tonic-gate 		fprintf(stderr,  "Warning: DKIOCADDBAD io control failed. System must be re-booted\n");
2407c478bd9Sstevel@tonic-gate 		fprintf(stderr, "for alternate sectors to be usable.\n");
2417c478bd9Sstevel@tonic-gate 		exit(17);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	sync();
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	fclose(badsecfd);
2467c478bd9Sstevel@tonic-gate 	close (alts_fd);
2477c478bd9Sstevel@tonic-gate 	close (devfd);
248*052b6e8aSbg159949 	return(0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Giveusage ()
2537c478bd9Sstevel@tonic-gate  * Give a (not so) concise message on how to use this program.
2547c478bd9Sstevel@tonic-gate  */
255*052b6e8aSbg159949 static
256*052b6e8aSbg159949 void giveusage(void)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s [-p] [-a sector] [-f filename] raw-device\n", progname);
2597c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	p - Print existing bad block map\n");
2607c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	a - Add the given sectors to the bad block list\n");
2617c478bd9Sstevel@tonic-gate 	fprintf(stderr, "	f - Add the sectors from <filename> to the bad block list\n");
2627c478bd9Sstevel@tonic-gate 	if (devfd)
2637c478bd9Sstevel@tonic-gate 		close(devfd);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  *	read in the additional growing bad sectors
2697c478bd9Sstevel@tonic-gate  */
270*052b6e8aSbg159949 static void
271*052b6e8aSbg159949 rd_gbad(FILE *badsecfd)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	int	badsec_entry;
2747c478bd9Sstevel@tonic-gate 	int	status;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	status = fscanf(badsecfd, "%d", &badsec_entry);
2777c478bd9Sstevel@tonic-gate 	while (status!=EOF) {
2787c478bd9Sstevel@tonic-gate 		add_gbad(badsec_entry);
2797c478bd9Sstevel@tonic-gate 		status = fscanf(badsecfd, "%d", &badsec_entry);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
283*052b6e8aSbg159949 static void
284*052b6e8aSbg159949 add_gbad(int badsec_entry)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (!gbadsl_chain) {
2897c478bd9Sstevel@tonic-gate 		blc_p = (struct badsec_lst *)malloc(BADSLSZ);
2907c478bd9Sstevel@tonic-gate 		if (!blc_p) {
2917c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unable to allocate memory for additional bad sectors\n");
2927c478bd9Sstevel@tonic-gate 			exit(18);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		gbadsl_chain = blc_p;
2957c478bd9Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
2967c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
2997c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (blc_p->bl_cnt == MAXBLENT) {
3027c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = (struct badsec_lst *)malloc(BADSLSZ);
3037c478bd9Sstevel@tonic-gate 		if (!blc_p->bl_nxt) {
3047c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unable to allocate memory for additional bad sectors\n");
3057c478bd9Sstevel@tonic-gate 			exit(19);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
3087c478bd9Sstevel@tonic-gate 		blc_p->bl_cnt = 0;
3097c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = 0;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	blc_p->bl_sec[blc_p->bl_cnt++] = badsec_entry;
3127c478bd9Sstevel@tonic-gate 	gbadsl_chain_cnt++;
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate  * Map a block using hardware (SCSI) techniques.
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3197c478bd9Sstevel@tonic-gate int
3207c478bd9Sstevel@tonic-gate hardware_remap (bn)
3217c478bd9Sstevel@tonic-gate int	bn;
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	u_int	byte_swap_32 (u_int);
3247c478bd9Sstevel@tonic-gate 	u_short	byte_swap_16 (u_short);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	struct uscsi_cmd		ucmd;
3277c478bd9Sstevel@tonic-gate 	union scsi_cdb			cdb;
3287c478bd9Sstevel@tonic-gate 	struct scsi_reassign_blk	defect_list;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Build and execute the uscsi ioctl
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 	(void) memset((char *)&ucmd, 0, sizeof (ucmd));
3347c478bd9Sstevel@tonic-gate 	(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
3357c478bd9Sstevel@tonic-gate 	(void) memset((char *)&defect_list, 0,
3367c478bd9Sstevel@tonic-gate 		sizeof (struct scsi_reassign_blk));
3377c478bd9Sstevel@tonic-gate 	cdb.scc_cmd = SCMD_REASSIGN_BLOCK;
3387c478bd9Sstevel@tonic-gate 	ucmd.uscsi_cdb = (caddr_t) &cdb;
3397c478bd9Sstevel@tonic-gate 	ucmd.uscsi_cdblen = CDB_GROUP0;
3407c478bd9Sstevel@tonic-gate 	ucmd.uscsi_bufaddr = (caddr_t) &defect_list;
3417c478bd9Sstevel@tonic-gate 	ucmd.uscsi_buflen = sizeof (struct scsi_reassign_blk);
3427c478bd9Sstevel@tonic-gate 	defect_list.length = byte_swap_16 (sizeof (defect_list.defect));
3437c478bd9Sstevel@tonic-gate 	defect_list.defect = byte_swap_32 (bn);
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	printf ("length - %x %x\n", sizeof (defect_list.defect), defect_list.length);
3467c478bd9Sstevel@tonic-gate 	printf ("defect - %x %x\n", bn, defect_list.defect);
3477c478bd9Sstevel@tonic-gate 	*/
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * Set function flags for driver.
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_DIAGNOSE | USCSI_SILENT;
3527c478bd9Sstevel@tonic-gate 	ucmd.uscsi_timeout = 30;		/* 30 seconds */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Execute the ioctl
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 	if (ioctl(devfd, USCSICMD, &ucmd) == -1)
3587c478bd9Sstevel@tonic-gate 	{
3597c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY)
3607c478bd9Sstevel@tonic-gate 		{
3617c478bd9Sstevel@tonic-gate 			perror ("SCSI hardware re-assign failed");
3627c478bd9Sstevel@tonic-gate 			/*
3637c478bd9Sstevel@tonic-gate 			 * It looks like a failure but by returning success
3647c478bd9Sstevel@tonic-gate 			 * the upper layer will not try to do
3657c478bd9Sstevel@tonic-gate 			 * software remapping.
3667c478bd9Sstevel@tonic-gate 			 */
3677c478bd9Sstevel@tonic-gate 			return (SUCCESS);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		return (FAILURE);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	return (SUCCESS);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate u_int
3757c478bd9Sstevel@tonic-gate byte_swap_32 (u_int nav)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	u_int	rc;
3787c478bd9Sstevel@tonic-gate 	rc = ((nav & 0xff000000) >> 24) | ((nav & 0x00ff0000) >> 8) |
3797c478bd9Sstevel@tonic-gate 	     ((nav & 0x0000ff00) << 8)  | ((nav & 0x000000ff) << 24);
3807c478bd9Sstevel@tonic-gate 	return (rc);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate u_short
3847c478bd9Sstevel@tonic-gate byte_swap_16 (u_short niv)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	u_short	rc;
3877c478bd9Sstevel@tonic-gate 	rc = (u_short)((int)(niv & 0xff00) >> 8) | ((niv & 0x00ff) << 8);
3887c478bd9Sstevel@tonic-gate 	return (rc);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
391*052b6e8aSbg159949 int
3927c478bd9Sstevel@tonic-gate try_hw_remap ()
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	struct badsec_lst *blc_p;
3957c478bd9Sstevel@tonic-gate 	int	i;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p != 0; blc_p = blc_p->bl_nxt)
3987c478bd9Sstevel@tonic-gate 	{
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		for (i = 0; i < blc_p->bl_cnt; i++)
4017c478bd9Sstevel@tonic-gate 		if (hardware_remap (blc_p->bl_sec[i]) == FAILURE)
4027c478bd9Sstevel@tonic-gate 			return (FAILURE);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	return (SUCCESS);
4057c478bd9Sstevel@tonic-gate }
406