xref: /titanic_54/usr/src/cmd/format/defect.c (revision e8fb11a1575b42dcd5c49341c588d9f6cd636135)
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*e8fb11a1Sshidokht  * Common Development and Distribution License (the "License").
6*e8fb11a1Sshidokht  * 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*e8fb11a1Sshidokht  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains routines that manipulate the defect list.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include "global.h"
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #if defined(sparc)
367c478bd9Sstevel@tonic-gate #include <sys/hdio.h>
377c478bd9Sstevel@tonic-gate #endif		/* defined(sparc) */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/buf.h>
407c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <memory.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #if defined(sparc)
487c478bd9Sstevel@tonic-gate #include <sys/dkbad.h>
497c478bd9Sstevel@tonic-gate #endif		/* defined(sparc) */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "misc.h"
527c478bd9Sstevel@tonic-gate #include "param.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #if defined(sparc)
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This structure is the bad block table for the current disk if
587c478bd9Sstevel@tonic-gate  * the disk uses bad-144 defect mapping.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate struct	dkbad badmap;
617c478bd9Sstevel@tonic-gate #endif		/* defined(sparc) */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * This routine reads the defect list off the disk.  It also reads in the
657c478bd9Sstevel@tonic-gate  * bad block table if the disk is a BAD144 type.  The defect list is
667c478bd9Sstevel@tonic-gate  * located on the first 2 tracks of the 2nd alternate cylinder of all
677c478bd9Sstevel@tonic-gate  * disks.  The bad block map is located on the first 5 even sectors of
687c478bd9Sstevel@tonic-gate  * the last track of the last cylinder.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate void
717c478bd9Sstevel@tonic-gate read_list(struct defect_list *list)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int	size, head;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #if defined(sparc)
767c478bd9Sstevel@tonic-gate 	int sec, status;
777c478bd9Sstevel@tonic-gate 	struct	bt_bad *bt;
787c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	assert(!EMBEDDED_SCSI);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * This flags has been introduced only for Sparc ATA IDE.
847c478bd9Sstevel@tonic-gate 	 * This indicates that no list manipulation is done in this controller
857c478bd9Sstevel@tonic-gate 	 * and hence return without any other checking.
867c478bd9Sstevel@tonic-gate 	 */
877c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_NOWLIST) {
887c478bd9Sstevel@tonic-gate 		return;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * Panther's working list is maintained by the controller
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_WLIST) {
957c478bd9Sstevel@tonic-gate 		if (*cur_ops->op_ex_cur != NULL &&
967c478bd9Sstevel@tonic-gate 		    ((*cur_ops->op_ex_cur)(list)) == 0) {
977c478bd9Sstevel@tonic-gate 			if (list->header.magicno != DEFECT_MAGIC) {
987c478bd9Sstevel@tonic-gate 				fmt_print("Defect list BAD\n");
997c478bd9Sstevel@tonic-gate 			} else {
1007c478bd9Sstevel@tonic-gate 				fmt_print("Controller working list found\n");
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 			return;
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		if (*cur_ops->op_ex_man != NULL &&
1067c478bd9Sstevel@tonic-gate 		    ((*cur_ops->op_ex_man)(list)) == 0) {
1077c478bd9Sstevel@tonic-gate 			if (list->header.magicno != DEFECT_MAGIC) {
1087c478bd9Sstevel@tonic-gate 				fmt_print("Defect list BAD\n");
1097c478bd9Sstevel@tonic-gate 			} else {
1107c478bd9Sstevel@tonic-gate 				fmt_print("MANUFACTURER's list found\n");
1117c478bd9Sstevel@tonic-gate 			}
1127c478bd9Sstevel@tonic-gate 			return;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		fmt_print("No defect list found\n");
1157c478bd9Sstevel@tonic-gate 		return;
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * Loop for each copy of the defect list until we get a good one.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	for (head = 0; head < LISTCOUNT; head++) {
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * Try to read the list header.
1247c478bd9Sstevel@tonic-gate 		 */
1257c478bd9Sstevel@tonic-gate 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file,
1267c478bd9Sstevel@tonic-gate 		    (diskaddr_t)chs2bn(ncyl + 1, head, 0), 1,
1277c478bd9Sstevel@tonic-gate 		    (char *)&list->header, NULL), F_NORMAL)
1287c478bd9Sstevel@tonic-gate 			continue;
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 		 * If the magic number is wrong, this copy is corrupt.
1317c478bd9Sstevel@tonic-gate 		 */
1327c478bd9Sstevel@tonic-gate 		if (list->header.magicno != DEFECT_MAGIC)
1337c478bd9Sstevel@tonic-gate 			continue;
1347c478bd9Sstevel@tonic-gate 		/*
1357c478bd9Sstevel@tonic-gate 		 * Allocate space for the rest of the list.
1367c478bd9Sstevel@tonic-gate 		 */
1377c478bd9Sstevel@tonic-gate 		size = LISTSIZE(list->header.count);
1387c478bd9Sstevel@tonic-gate 		list->list = (struct defect_entry *)zalloc(size * SECSIZE);
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * Try to read in the rest of the list. If there is an
1417c478bd9Sstevel@tonic-gate 		 * error, or the checksum is wrong, this copy is corrupt.
1427c478bd9Sstevel@tonic-gate 		 */
1437c478bd9Sstevel@tonic-gate 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file,
1447c478bd9Sstevel@tonic-gate 		    (diskaddr_t)chs2bn(ncyl + 1, head, 1), size,
1457c478bd9Sstevel@tonic-gate 		    (char *)list->list, F_NORMAL, NULL) ||
1467c478bd9Sstevel@tonic-gate 		    checkdefsum(list, CK_CHECKSUM)) {
1477c478bd9Sstevel@tonic-gate 			/*
1487c478bd9Sstevel@tonic-gate 			 * Destroy the list and go on.
1497c478bd9Sstevel@tonic-gate 			 */
1507c478bd9Sstevel@tonic-gate 			kill_deflist(list);
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		/*
1547c478bd9Sstevel@tonic-gate 		 * Got a good copy, stop searching.
1557c478bd9Sstevel@tonic-gate 		 */
1567c478bd9Sstevel@tonic-gate 		break;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate #if defined(sparc)
1597c478bd9Sstevel@tonic-gate 	if (!(cur_ctlr->ctlr_flags & DKI_BAD144))
1607c478bd9Sstevel@tonic-gate 		return;
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * The disk uses BAD144, read in the bad-block table.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	for (sec = 0; ((sec < BAD_LISTCNT * 2) && (sec < nsect)); sec += 2) {
1657c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file,
1667c478bd9Sstevel@tonic-gate 		    (diskaddr_t)chs2bn(ncyl + acyl - 1, nhead - 1, sec), 1,
1677c478bd9Sstevel@tonic-gate 		    &badmap, F_NORMAL, NULL);
1687c478bd9Sstevel@tonic-gate 		if (status)
1697c478bd9Sstevel@tonic-gate 			continue;
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * Do a sanity check on the list read in.  If it passes,
1727c478bd9Sstevel@tonic-gate 		 * stop searching.
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		if (badmap.bt_mbz != 0)
1757c478bd9Sstevel@tonic-gate 			continue;
1767c478bd9Sstevel@tonic-gate 		for (bt = badmap.bt_bad; bt - badmap.bt_bad < NDKBAD; bt++) {
1777c478bd9Sstevel@tonic-gate 			if (bt->bt_cyl < 0)
1787c478bd9Sstevel@tonic-gate 				break;
1797c478bd9Sstevel@tonic-gate 			if (bt->bt_trksec < 0)
1807c478bd9Sstevel@tonic-gate 				continue;
1817c478bd9Sstevel@tonic-gate 			head = bt->bt_trksec >> 8;
1827c478bd9Sstevel@tonic-gate 			if ((bt->bt_cyl >= pcyl) || (head >= nhead) ||
1837c478bd9Sstevel@tonic-gate 			    ((bt->bt_trksec & 0xff) >= sectors(head))) {
1847c478bd9Sstevel@tonic-gate 				status = -1;
1857c478bd9Sstevel@tonic-gate 				break;
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (status)
1897c478bd9Sstevel@tonic-gate 			continue;
1907c478bd9Sstevel@tonic-gate 		return;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * If we couldn't find the bad block table, initialize it to
1947c478bd9Sstevel@tonic-gate 	 * zero entries.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	for (bt = badmap.bt_bad; bt - badmap.bt_bad < NDKBAD; bt++)
1977c478bd9Sstevel@tonic-gate 		bt->bt_cyl = bt->bt_trksec = -1;
1987c478bd9Sstevel@tonic-gate 	badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
1997c478bd9Sstevel@tonic-gate #endif		/* defined(sparc) */
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * This routine either checks or calculates the checksum for a defect
2047c478bd9Sstevel@tonic-gate  * list, depending on the mode parameter. In check mode, it returns
2057c478bd9Sstevel@tonic-gate  * whether or not the checksum is correct.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate int
2087c478bd9Sstevel@tonic-gate checkdefsum(struct defect_list *list, int mode)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	register int *lp, i, sum = 0;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Perform the rolling xor to get what the checksum should be.
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	lp = (int *)list->list;
2167c478bd9Sstevel@tonic-gate 	for (i = 0; i < (list->header.count *
2177c478bd9Sstevel@tonic-gate 			sizeof (struct defect_entry) / sizeof (int)); i++)
2187c478bd9Sstevel@tonic-gate 		sum ^= *(lp + i);
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * If in check mode, return whether header checksum was correct.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	if (mode == CK_CHECKSUM)
2237c478bd9Sstevel@tonic-gate 		return (sum != list->header.cksum);
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * If in create mode, set the header checksum.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	else {
2287c478bd9Sstevel@tonic-gate 		list->header.cksum = sum;
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * This routine prints a single defect to stdout in a readable format.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate void
2377c478bd9Sstevel@tonic-gate pr_defect(struct defect_entry *def, int num)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Make defect numbering look 1 relative.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	++num;
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Print out common values.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	fmt_print("%4d%8d%7d", num, def->cyl, def->head);
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * The rest of the values may be unknown. If they are, just
2507c478bd9Sstevel@tonic-gate 	 * print blanks instead.  Also, only print length only if bfi is
2517c478bd9Sstevel@tonic-gate 	 * known, and assume that a known bfi implies an unknown sect.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	if (def->bfi != UNKNOWN) {
2547c478bd9Sstevel@tonic-gate 		fmt_print("%8d", def->bfi);
2557c478bd9Sstevel@tonic-gate 		if (def->nbits != UNKNOWN)
2567c478bd9Sstevel@tonic-gate 			fmt_print("%8d", def->nbits);
2577c478bd9Sstevel@tonic-gate 	} else {
2587c478bd9Sstevel@tonic-gate 		fmt_print("                ");
2597c478bd9Sstevel@tonic-gate 		fmt_print("%8d", def->sect);
2607c478bd9Sstevel@tonic-gate 		fmt_print("%8lu", chs2bn(def->cyl, def->head, def->sect));
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	fmt_print("\n");
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * This routine calculates where in a defect list a given defect should
2677c478bd9Sstevel@tonic-gate  * be sorted. It returns the index that the defect should become.  The
2687c478bd9Sstevel@tonic-gate  * algorithm used sorts all bfi based defects by cylinder/head/bfi, and
2697c478bd9Sstevel@tonic-gate  * adds all logical sector defects to the end of the list.  This is
2707c478bd9Sstevel@tonic-gate  * necessary because the ordering of logical sector defects is significant
2717c478bd9Sstevel@tonic-gate  * when sector slipping is employed.
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate int
2747c478bd9Sstevel@tonic-gate sort_defect(struct defect_entry *def, struct defect_list *list)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	struct	defect_entry *ptr;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * If it's a logical sector defect, return the entry at the end
2807c478bd9Sstevel@tonic-gate 	 * of the list.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	if (def->bfi == UNKNOWN)
2837c478bd9Sstevel@tonic-gate 		return (list->header.count);
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * It's a bfi defect.  Loop through the defect list.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	for (ptr = list->list; ptr - list->list < list->header.count; ptr++) {
2887c478bd9Sstevel@tonic-gate 		/*
2897c478bd9Sstevel@tonic-gate 		 * If we get to a logical sector defect, put this defect
2907c478bd9Sstevel@tonic-gate 		 * right before it.
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		if (ptr->bfi == UNKNOWN)
2937c478bd9Sstevel@tonic-gate 			goto found;
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * If we get to a defect that is past this one in
2967c478bd9Sstevel@tonic-gate 		 * cylinder/head/bfi, put this defect right before it.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 		if (def->cyl < ptr->cyl)
2997c478bd9Sstevel@tonic-gate 			goto found;
3007c478bd9Sstevel@tonic-gate 		if (def->cyl != ptr->cyl)
3017c478bd9Sstevel@tonic-gate 			continue;
3027c478bd9Sstevel@tonic-gate 		if (def->head < ptr->head)
3037c478bd9Sstevel@tonic-gate 			goto found;
3047c478bd9Sstevel@tonic-gate 		if (def->head != ptr->head)
3057c478bd9Sstevel@tonic-gate 			continue;
3067c478bd9Sstevel@tonic-gate 		if (def->bfi < ptr->bfi)
3077c478bd9Sstevel@tonic-gate 			goto found;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate found:
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Return the index to put the defect at.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	return (ptr - list->list);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * This routine writes the defect list on the back on the disk.  It also
3187c478bd9Sstevel@tonic-gate  * writes the bad block table to disk if bad-144 mapping applies to the
3197c478bd9Sstevel@tonic-gate  * current disk.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate void
3227c478bd9Sstevel@tonic-gate write_deflist(struct defect_list *list)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int	size, head, status;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate #if defined(sparc)
3277c478bd9Sstevel@tonic-gate 	int sec;
3287c478bd9Sstevel@tonic-gate 	caddr_t	bad_ptr = (caddr_t)&badmap;
3297c478bd9Sstevel@tonic-gate #endif			/* defined(sparc) */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	assert(!EMBEDDED_SCSI);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Sparc ATA IDE.
3357c478bd9Sstevel@tonic-gate 	 * This indicates that no list manipulation is done in this controller
3367c478bd9Sstevel@tonic-gate 	 * and hence return without any other checking.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_NOWLIST) {
3397c478bd9Sstevel@tonic-gate 		return;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Panther's working list is maintained by the controller
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_flags & CF_WLIST) {
3467c478bd9Sstevel@tonic-gate 		(*cur_ops->op_wr_cur)(list);
3477c478bd9Sstevel@tonic-gate 		return;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * If the list is null, there is nothing to write.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if (list->list != NULL) {
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * calculate how many sectors the defect list will occupy.
3567c478bd9Sstevel@tonic-gate 		 */
3577c478bd9Sstevel@tonic-gate 		size = LISTSIZE(list->header.count);
3587c478bd9Sstevel@tonic-gate 		/*
3597c478bd9Sstevel@tonic-gate 		 * Loop for each copy of the list to be written.  Write
3607c478bd9Sstevel@tonic-gate 		 * out the header of the list followed by the data.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		for (head = 0; head < LISTCOUNT; head++) {
3637c478bd9Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3647c478bd9Sstevel@tonic-gate 			    (diskaddr_t)chs2bn(ncyl + 1, head, 0), 1,
3657c478bd9Sstevel@tonic-gate 			    (char *)&list->header, F_NORMAL, NULL);
3667c478bd9Sstevel@tonic-gate 			if (status) {
3677c478bd9Sstevel@tonic-gate 				err_print(
3687c478bd9Sstevel@tonic-gate "Warning: error saving defect list.\n");
3697c478bd9Sstevel@tonic-gate 				continue;
3707c478bd9Sstevel@tonic-gate 			}
3717c478bd9Sstevel@tonic-gate 			status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3727c478bd9Sstevel@tonic-gate 			    (diskaddr_t)chs2bn(ncyl + 1, head, 1), size,
3737c478bd9Sstevel@tonic-gate 			    (char *)list->list, F_NORMAL, NULL);
3747c478bd9Sstevel@tonic-gate 			if (status)
3757c478bd9Sstevel@tonic-gate 				err_print(
3767c478bd9Sstevel@tonic-gate "Warning: error saving defect list.\n");
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	if (!(cur_ctlr->ctlr_flags & DKI_BAD144))
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate #if defined(sparc)
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Current disk uses bad-144 mapping.  Loop for each copy of the
3847c478bd9Sstevel@tonic-gate 	 * bad block table to be written and write it out.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	for (sec = 0; ((sec < BAD_LISTCNT * 2) && (sec < nsect)); sec += 2) {
3877c478bd9Sstevel@tonic-gate 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3887c478bd9Sstevel@tonic-gate 		    (diskaddr_t)chs2bn(ncyl + acyl - 1, nhead - 1, sec), 1,
3897c478bd9Sstevel@tonic-gate 		    &badmap, F_NORMAL, NULL);
3907c478bd9Sstevel@tonic-gate 		if (status) {
3917c478bd9Sstevel@tonic-gate 			err_print(
3927c478bd9Sstevel@tonic-gate "Warning: error saving bad block map table.\n");
3937c478bd9Sstevel@tonic-gate 			continue;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Execute an ioctl to tell unix about the new bad block table.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr))
4007c478bd9Sstevel@tonic-gate 		err_print(
4017c478bd9Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n");
4027c478bd9Sstevel@tonic-gate #endif		/* defined(sparc) */
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * This routine adds a logical sector to the given defect list.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate void
4097c478bd9Sstevel@tonic-gate add_ldef(diskaddr_t blkno, struct defect_list *list)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	struct	defect_entry def;
4127c478bd9Sstevel@tonic-gate 	int	index;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Calculate the fields for the defect struct.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	def.cyl = bn2c(blkno);
4197c478bd9Sstevel@tonic-gate 	def.head = bn2h(blkno);
4207c478bd9Sstevel@tonic-gate 	def.sect = bn2s(blkno);
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Initialize the unknown fields.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	def.bfi = def.nbits = UNKNOWN;
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Calculate the index into the list that the defect belongs at.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	index = sort_defect(&def, list);
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Add the defect to the list.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	add_def(&def, list, index);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * This routine adds the given defect struct to the defect list at
4377c478bd9Sstevel@tonic-gate  * a precalculated index.
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate void
4407c478bd9Sstevel@tonic-gate add_def(struct defect_entry *def, struct defect_list *list, int index)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	int	count, i;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * If adding this defect makes the list overflow into another
4467c478bd9Sstevel@tonic-gate 	 * sector, allocate the necessary space.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	count = list->header.count;
4497c478bd9Sstevel@tonic-gate 	if (LISTSIZE(count + 1) > LISTSIZE(count))
4507c478bd9Sstevel@tonic-gate 		list->list = (struct defect_entry *)rezalloc((void *)list->list,
4517c478bd9Sstevel@tonic-gate 		    LISTSIZE(count + 1) * SECSIZE);
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * Slip all the defects after this one down one slot in the list.
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	for (i = count; i > index; i--)
4567c478bd9Sstevel@tonic-gate 		*(list->list + i) = *(list->list + i - 1);
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Fill in the created hole with this defect.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	*(list->list + i) = *def;
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * Increment the count and calculate a new checksum.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	list->header.count++;
4657c478bd9Sstevel@tonic-gate 	(void) checkdefsum(list, CK_MAKESUM);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * This routine sets the given defect list back to null.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate void
4727c478bd9Sstevel@tonic-gate kill_deflist(struct defect_list *list)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * If it's already null, we're done.
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	if (list->list == NULL)
4797c478bd9Sstevel@tonic-gate 		return;
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Free the malloc'd space it's using.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	destroy_data((char *)list->list);
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Mark it as null, and clear any flags.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	list->list = NULL;
4887c478bd9Sstevel@tonic-gate 	list->flags = 0;
4897c478bd9Sstevel@tonic-gate }
490