17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*cfcc56e0SMark Logan * Common Development and Distribution License (the "License"). 6*cfcc56e0SMark Logan * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*cfcc56e0SMark Logan * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * Copyrighted as an unpublished work. 327c478bd9Sstevel@tonic-gate * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990 337c478bd9Sstevel@tonic-gate * All rights reserved. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <fcntl.h> 387c478bd9Sstevel@tonic-gate #include <memory.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/param.h> 417c478bd9Sstevel@tonic-gate #include <sys/stat.h> 427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 437c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 447c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 46*cfcc56e0SMark Logan #include <stdlib.h> 47*cfcc56e0SMark Logan #include <strings.h> 48*cfcc56e0SMark Logan #include <unistd.h> 49*cfcc56e0SMark Logan #include <stropts.h> 507c478bd9Sstevel@tonic-gate #include <sys/scsi/generic/commands.h> 517c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/commands.h> 527c478bd9Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h> 537c478bd9Sstevel@tonic-gate #include "badsec.h" 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate char *devname; /* name of device */ 567c478bd9Sstevel@tonic-gate int devfd; /* device file descriptor */ 577c478bd9Sstevel@tonic-gate struct dk_geom dkg; /* geometry */ 58*cfcc56e0SMark Logan struct extvtoc vtoc; /* table of contents */ 597c478bd9Sstevel@tonic-gate char *progname; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate extern struct badsec_lst *badsl_chain; 627c478bd9Sstevel@tonic-gate extern int badsl_chain_cnt; 637c478bd9Sstevel@tonic-gate extern struct badsec_lst *gbadsl_chain; 647c478bd9Sstevel@tonic-gate extern int gbadsl_chain_cnt; 65*cfcc56e0SMark Logan extern int print_altsec(struct extpartition *); 66*cfcc56e0SMark Logan extern int updatebadsec(struct extpartition *, int); 67*cfcc56e0SMark Logan extern void wr_altsctr(void); 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate int alts_fd; 707c478bd9Sstevel@tonic-gate 71052b6e8aSbg159949 static void giveusage(void); 72052b6e8aSbg159949 static void rd_gbad(FILE *badsecfd); 73052b6e8aSbg159949 static void add_gbad(int badsec_entry); 74*cfcc56e0SMark Logan static int try_hw_remap(void); 75*cfcc56e0SMark Logan static int hardware_remap(blkaddr_t); 76052b6e8aSbg159949 77052b6e8aSbg159949 int 78052b6e8aSbg159949 main(int argc, char *argv[]) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate extern int optind; 817c478bd9Sstevel@tonic-gate extern char *optarg; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static char options[] = "Ipa:f:"; 847c478bd9Sstevel@tonic-gate char numbuf[100]; 857c478bd9Sstevel@tonic-gate char *nxtarg; 867c478bd9Sstevel@tonic-gate char *alts_name; 877c478bd9Sstevel@tonic-gate minor_t minor_val; 887c478bd9Sstevel@tonic-gate struct stat statbuf; 89*cfcc56e0SMark Logan struct extpartition *part = NULL; 907c478bd9Sstevel@tonic-gate int alts_slice = -1; 917c478bd9Sstevel@tonic-gate int l; 927c478bd9Sstevel@tonic-gate int p; 937c478bd9Sstevel@tonic-gate int init_flag = 0; 947c478bd9Sstevel@tonic-gate int print_flag = 0; 957c478bd9Sstevel@tonic-gate int c; 967c478bd9Sstevel@tonic-gate int i; 977c478bd9Sstevel@tonic-gate FILE *badsecfd = NULL; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate progname = argv[0]; 1007c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, options)) != EOF) { 1017c478bd9Sstevel@tonic-gate switch (c) { 1027c478bd9Sstevel@tonic-gate case 'I': 1037c478bd9Sstevel@tonic-gate init_flag = 1; 1047c478bd9Sstevel@tonic-gate break; 1057c478bd9Sstevel@tonic-gate case 'p': 1067c478bd9Sstevel@tonic-gate print_flag = 1; 1077c478bd9Sstevel@tonic-gate break; 1087c478bd9Sstevel@tonic-gate case 'a': 1097c478bd9Sstevel@tonic-gate nxtarg = optarg; 1107c478bd9Sstevel@tonic-gate for (; *nxtarg != '\0'; ) 1117c478bd9Sstevel@tonic-gate add_gbad(strtol(nxtarg, &nxtarg, 0)); 1127c478bd9Sstevel@tonic-gate break; 1137c478bd9Sstevel@tonic-gate case 'f': 1147c478bd9Sstevel@tonic-gate if ((badsecfd = fopen(optarg, "r")) == NULL) { 115*cfcc56e0SMark Logan (void) fprintf(stderr, 116*cfcc56e0SMark Logan "%s: unable to open %s file\n", 117*cfcc56e0SMark Logan progname, optarg); 1187c478bd9Sstevel@tonic-gate exit(1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate default: 1227c478bd9Sstevel@tonic-gate giveusage(); 1237c478bd9Sstevel@tonic-gate exit(2); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* get the last argument -- device stanza */ 1287c478bd9Sstevel@tonic-gate if (argc != optind+1) { 129*cfcc56e0SMark Logan (void) fprintf(stderr, "Missing disk device name\n"); 1307c478bd9Sstevel@tonic-gate giveusage(); 1317c478bd9Sstevel@tonic-gate exit(3); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate devname = argv[optind]; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate if (stat(devname, &statbuf)) { 136*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: invalid device %s, stat failed\n", 137*cfcc56e0SMark Logan progname, devname); 1387c478bd9Sstevel@tonic-gate giveusage(); 1397c478bd9Sstevel@tonic-gate exit(4); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 142*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: device %s is not character" 143*cfcc56e0SMark Logan " special\n", progname, devname); 1447c478bd9Sstevel@tonic-gate giveusage(); 1457c478bd9Sstevel@tonic-gate exit(5); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate minor_val = minor(statbuf.st_rdev); 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * NEED A DEFINE FOR THE PHYSICAL BIT (0x10) 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate if ((minor_val & 0x10) == 0) { 152*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: device %s is not a physical" 153*cfcc56e0SMark Logan " slice\n", progname, devname); 1547c478bd9Sstevel@tonic-gate giveusage(); 1557c478bd9Sstevel@tonic-gate exit(6); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate if ((minor_val % V_NUMPAR) != 0) { 158*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: device %s is not a slice 0" 159*cfcc56e0SMark Logan " device\n", progname, devname); 1607c478bd9Sstevel@tonic-gate giveusage(); 1617c478bd9Sstevel@tonic-gate exit(7); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate if ((devfd = open(devname, O_RDWR)) == -1) { 164*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: open of %s failed\n", 165*cfcc56e0SMark Logan progname, devname); 1667c478bd9Sstevel@tonic-gate perror(""); 1677c478bd9Sstevel@tonic-gate exit(8); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate if ((ioctl(devfd, DKIOCGGEOM, &dkg)) == -1) { 170*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: unable to get disk geometry.\n", 171*cfcc56e0SMark Logan progname); 1727c478bd9Sstevel@tonic-gate perror(""); 1737c478bd9Sstevel@tonic-gate exit(9); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 176*cfcc56e0SMark Logan if (ioctl(devfd, DKIOCGEXTVTOC, &vtoc) == -1) { 177*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: could not get VTOC.\n", progname); 1787c478bd9Sstevel@tonic-gate giveusage(); 1797c478bd9Sstevel@tonic-gate exit(14); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if ((vtoc.v_sanity != VTOC_SANE) || (vtoc.v_version != V_VERSION)) { 183*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: invalid VTOC found.\n", progname); 1847c478bd9Sstevel@tonic-gate giveusage(); 1857c478bd9Sstevel@tonic-gate exit(15); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate if (badsecfd) 1887c478bd9Sstevel@tonic-gate rd_gbad(badsecfd); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate #ifdef ADDBAD_DEBUG 191*cfcc56e0SMark Logan { 192*cfcc56e0SMark Logan struct badsec_lst *blc_p; 1937c478bd9Sstevel@tonic-gate printf("\n main: Total bad sectors found= %d\n", gbadsl_chain_cnt); 1947c478bd9Sstevel@tonic-gate for (blc_p = gbadsl_chain; blc_p; blc_p = blc_p->bl_nxt) { 1957c478bd9Sstevel@tonic-gate for (i = 0; i < blc_p->bl_cnt; i++) 1967c478bd9Sstevel@tonic-gate printf(" badsec=%d ", blc_p->bl_sec[i]); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate printf("\n"); 199*cfcc56e0SMark Logan } 2007c478bd9Sstevel@tonic-gate #endif 2017c478bd9Sstevel@tonic-gate #ifdef PPP 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * If init_flag is set, run to completion. 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate if (gbadsl_chain_cnt == 0 && init_flag == 0) 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * No defects and not initializing 2087c478bd9Sstevel@tonic-gate */ 2097c478bd9Sstevel@tonic-gate exit(0); 2107c478bd9Sstevel@tonic-gate #endif 2117c478bd9Sstevel@tonic-gate if (gbadsl_chain_cnt != 0) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate if (try_hw_remap() == SUCCESS) 2147c478bd9Sstevel@tonic-gate exit(0); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * get ALTS slice 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR && alts_slice == -1; i++) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate if (vtoc.v_part[i].p_tag == V_ALTSCTR) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate alts_slice = i; 2247c478bd9Sstevel@tonic-gate part = &vtoc.v_part[i]; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate if (alts_slice == -1) 2287c478bd9Sstevel@tonic-gate { 229*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: No alternates slice.\n", progname); 2307c478bd9Sstevel@tonic-gate exit(16); 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate l = strlen(devname); 233*cfcc56e0SMark Logan (void) sprintf(numbuf, "%d", alts_slice); 2347c478bd9Sstevel@tonic-gate p = strlen(numbuf); 2357c478bd9Sstevel@tonic-gate alts_name = (char *)malloc(l + p); 236*cfcc56e0SMark Logan (void) strcpy(alts_name, devname); 2377c478bd9Sstevel@tonic-gate alts_name[l - 2] = 's'; 238*cfcc56e0SMark Logan (void) strcpy(&alts_name[l - 1], numbuf); 2397c478bd9Sstevel@tonic-gate alts_name[l + p - 1] = '\0'; 2407c478bd9Sstevel@tonic-gate if ((alts_fd = open(alts_name, O_RDWR)) == -1) { 241*cfcc56e0SMark Logan (void) fprintf(stderr, "%s: open of %s failed\n", 242*cfcc56e0SMark Logan progname, alts_name); 2437c478bd9Sstevel@tonic-gate perror(""); 2447c478bd9Sstevel@tonic-gate exit(9); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate if (print_flag) 2477c478bd9Sstevel@tonic-gate { 248*cfcc56e0SMark Logan (void) print_altsec(part); 2497c478bd9Sstevel@tonic-gate exit(0); 2507c478bd9Sstevel@tonic-gate } 251*cfcc56e0SMark Logan (void) updatebadsec(part, init_flag); 2527c478bd9Sstevel@tonic-gate wr_altsctr(); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (ioctl(devfd, DKIOCADDBAD, NULL) == -1) { 255*cfcc56e0SMark Logan (void) fprintf(stderr, "Warning: DKIOCADDBAD io control" 256*cfcc56e0SMark Logan " failed. System must be re-booted\n"); 257*cfcc56e0SMark Logan (void) fprintf(stderr, "for alternate sectors to be usable.\n"); 2587c478bd9Sstevel@tonic-gate exit(17); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate sync(); 2617c478bd9Sstevel@tonic-gate 262*cfcc56e0SMark Logan (void) fclose(badsecfd); 263*cfcc56e0SMark Logan (void) close(alts_fd); 264*cfcc56e0SMark Logan (void) close(devfd); 265052b6e8aSbg159949 return (0); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * Giveusage () 2707c478bd9Sstevel@tonic-gate * Give a (not so) concise message on how to use this program. 2717c478bd9Sstevel@tonic-gate */ 272*cfcc56e0SMark Logan static void 273*cfcc56e0SMark Logan giveusage(void) 2747c478bd9Sstevel@tonic-gate { 275*cfcc56e0SMark Logan (void) fprintf(stderr, "%s [-p] [-a sector] [-f filename]" 276*cfcc56e0SMark Logan " raw-device\n", progname); 277*cfcc56e0SMark Logan (void) fprintf(stderr, " p - Print existing bad block map\n"); 278*cfcc56e0SMark Logan (void) fprintf(stderr, " a - Add the given sectors to the" 279*cfcc56e0SMark Logan " bad block list\n"); 280*cfcc56e0SMark Logan (void) fprintf(stderr, " f - Add the sectors from <filename>" 281*cfcc56e0SMark Logan " to the bad block list\n"); 2827c478bd9Sstevel@tonic-gate if (devfd) 283*cfcc56e0SMark Logan (void) close(devfd); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * read in the additional growing bad sectors 2897c478bd9Sstevel@tonic-gate */ 290052b6e8aSbg159949 static void 291052b6e8aSbg159949 rd_gbad(FILE *badsecfd) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate int badsec_entry; 2947c478bd9Sstevel@tonic-gate int status; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate status = fscanf(badsecfd, "%d", &badsec_entry); 2977c478bd9Sstevel@tonic-gate while (status != EOF) { 2987c478bd9Sstevel@tonic-gate add_gbad(badsec_entry); 2997c478bd9Sstevel@tonic-gate status = fscanf(badsecfd, "%d", &badsec_entry); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 303052b6e8aSbg159949 static void 304052b6e8aSbg159949 add_gbad(int badsec_entry) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate struct badsec_lst *blc_p; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate if (!gbadsl_chain) { 3097c478bd9Sstevel@tonic-gate blc_p = (struct badsec_lst *)malloc(BADSLSZ); 3107c478bd9Sstevel@tonic-gate if (!blc_p) { 311*cfcc56e0SMark Logan (void) fprintf(stderr, "Unable to allocate memory" 312*cfcc56e0SMark Logan " for additional bad sectors\n"); 3137c478bd9Sstevel@tonic-gate exit(18); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate gbadsl_chain = blc_p; 3167c478bd9Sstevel@tonic-gate blc_p->bl_cnt = 0; 3177c478bd9Sstevel@tonic-gate blc_p->bl_nxt = 0; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate for (blc_p = gbadsl_chain; blc_p->bl_nxt; ) 3207c478bd9Sstevel@tonic-gate blc_p = blc_p->bl_nxt; 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate if (blc_p->bl_cnt == MAXBLENT) { 3237c478bd9Sstevel@tonic-gate blc_p->bl_nxt = (struct badsec_lst *)malloc(BADSLSZ); 3247c478bd9Sstevel@tonic-gate if (!blc_p->bl_nxt) { 325*cfcc56e0SMark Logan (void) fprintf(stderr, "Unable to allocate memory" 326*cfcc56e0SMark Logan " for additional bad sectors\n"); 3277c478bd9Sstevel@tonic-gate exit(19); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate blc_p = blc_p->bl_nxt; 3307c478bd9Sstevel@tonic-gate blc_p->bl_cnt = 0; 3317c478bd9Sstevel@tonic-gate blc_p->bl_nxt = 0; 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate blc_p->bl_sec[blc_p->bl_cnt++] = badsec_entry; 3347c478bd9Sstevel@tonic-gate gbadsl_chain_cnt++; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* 3387c478bd9Sstevel@tonic-gate * Map a block using hardware (SCSI) techniques. 3397c478bd9Sstevel@tonic-gate */ 3407c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 341*cfcc56e0SMark Logan static int 3427c478bd9Sstevel@tonic-gate hardware_remap(bn) 343*cfcc56e0SMark Logan blkaddr_t bn; 3447c478bd9Sstevel@tonic-gate { 345*cfcc56e0SMark Logan uint_t byte_swap_32(uint_t); 346*cfcc56e0SMark Logan ushort_t byte_swap_16(ushort_t); 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate struct uscsi_cmd ucmd; 3497c478bd9Sstevel@tonic-gate union scsi_cdb cdb; 3507c478bd9Sstevel@tonic-gate struct scsi_reassign_blk defect_list; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /* 3537c478bd9Sstevel@tonic-gate * Build and execute the uscsi ioctl 3547c478bd9Sstevel@tonic-gate */ 3557c478bd9Sstevel@tonic-gate (void) memset((char *)&ucmd, 0, sizeof (ucmd)); 3567c478bd9Sstevel@tonic-gate (void) memset((char *)&cdb, 0, sizeof (union scsi_cdb)); 3577c478bd9Sstevel@tonic-gate (void) memset((char *)&defect_list, 0, 3587c478bd9Sstevel@tonic-gate sizeof (struct scsi_reassign_blk)); 3597c478bd9Sstevel@tonic-gate cdb.scc_cmd = SCMD_REASSIGN_BLOCK; 3607c478bd9Sstevel@tonic-gate ucmd.uscsi_cdb = (caddr_t)&cdb; 3617c478bd9Sstevel@tonic-gate ucmd.uscsi_cdblen = CDB_GROUP0; 3627c478bd9Sstevel@tonic-gate ucmd.uscsi_bufaddr = (caddr_t)&defect_list; 3637c478bd9Sstevel@tonic-gate ucmd.uscsi_buflen = sizeof (struct scsi_reassign_blk); 3647c478bd9Sstevel@tonic-gate defect_list.length = byte_swap_16(sizeof (defect_list.defect)); 3657c478bd9Sstevel@tonic-gate defect_list.defect = byte_swap_32(bn); 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * Set function flags for driver. 3687c478bd9Sstevel@tonic-gate */ 3697c478bd9Sstevel@tonic-gate ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_DIAGNOSE | USCSI_SILENT; 3707c478bd9Sstevel@tonic-gate ucmd.uscsi_timeout = 30; /* 30 seconds */ 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* 3737c478bd9Sstevel@tonic-gate * Execute the ioctl 3747c478bd9Sstevel@tonic-gate */ 3757c478bd9Sstevel@tonic-gate if (ioctl(devfd, USCSICMD, &ucmd) == -1) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate if (errno != ENOTTY) 3787c478bd9Sstevel@tonic-gate { 3797c478bd9Sstevel@tonic-gate perror("SCSI hardware re-assign failed"); 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * It looks like a failure but by returning success 3827c478bd9Sstevel@tonic-gate * the upper layer will not try to do 3837c478bd9Sstevel@tonic-gate * software remapping. 3847c478bd9Sstevel@tonic-gate */ 3857c478bd9Sstevel@tonic-gate return (SUCCESS); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate return (FAILURE); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate return (SUCCESS); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 392*cfcc56e0SMark Logan uint_t 393*cfcc56e0SMark Logan byte_swap_32(uint_t nav) 3947c478bd9Sstevel@tonic-gate { 395*cfcc56e0SMark Logan uint_t rc; 3967c478bd9Sstevel@tonic-gate rc = ((nav & 0xff000000) >> 24) | ((nav & 0x00ff0000) >> 8) | 3977c478bd9Sstevel@tonic-gate ((nav & 0x0000ff00) << 8) | ((nav & 0x000000ff) << 24); 3987c478bd9Sstevel@tonic-gate return (rc); 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 401*cfcc56e0SMark Logan ushort_t 402*cfcc56e0SMark Logan byte_swap_16(ushort_t niv) 4037c478bd9Sstevel@tonic-gate { 404*cfcc56e0SMark Logan ushort_t rc; 405*cfcc56e0SMark Logan rc = (ushort_t)((int)(niv & 0xff00) >> 8) | ((niv & 0x00ff) << 8); 4067c478bd9Sstevel@tonic-gate return (rc); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 409*cfcc56e0SMark Logan static int 4107c478bd9Sstevel@tonic-gate try_hw_remap() 4117c478bd9Sstevel@tonic-gate { 4127c478bd9Sstevel@tonic-gate struct badsec_lst *blc_p; 4137c478bd9Sstevel@tonic-gate int i; 4147c478bd9Sstevel@tonic-gate 415*cfcc56e0SMark Logan for (blc_p = gbadsl_chain; blc_p != 0; blc_p = blc_p->bl_nxt) { 4167c478bd9Sstevel@tonic-gate for (i = 0; i < blc_p->bl_cnt; i++) 4177c478bd9Sstevel@tonic-gate if (hardware_remap(blc_p->bl_sec[i]) == FAILURE) 4187c478bd9Sstevel@tonic-gate return (FAILURE); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate return (SUCCESS); 4217c478bd9Sstevel@tonic-gate } 422