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