xref: /illumos-gate/usr/src/cmd/format/ctlr_ata.c (revision b12aaafb)
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
5342440ecSPrasad Singamsetty  * Common Development and Distribution License (the "License").
6342440ecSPrasad Singamsetty  * 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 /*
2265908c77Syu, 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 the routines for the IDE drive interface
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include "global.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/uio.h>
357c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
367c478bd9Sstevel@tonic-gate #include <memory.h>
377c478bd9Sstevel@tonic-gate #include <malloc.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #if defined(i386)
447c478bd9Sstevel@tonic-gate #include <sys/dktp/altsctr.h>
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate #include <sys/dktp/dadkio.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "startup.h"
507c478bd9Sstevel@tonic-gate #include "misc.h"
517c478bd9Sstevel@tonic-gate #include "ctlr_ata.h"
527c478bd9Sstevel@tonic-gate #include "analyze.h"
537c478bd9Sstevel@tonic-gate #include "param.h"
547c478bd9Sstevel@tonic-gate #include "io.h"
557c478bd9Sstevel@tonic-gate #include "badsec.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "menu_fdisk.h"
587c478bd9Sstevel@tonic-gate 
59a11d78d6SToomas Soome diskaddr_t altsec_offset;
60a11d78d6SToomas Soome 
617c478bd9Sstevel@tonic-gate int	wr_altsctr();
627c478bd9Sstevel@tonic-gate int	read_altsctr();
637c478bd9Sstevel@tonic-gate int	updatebadsec();
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int	ata_ck_format(void);
667c478bd9Sstevel@tonic-gate #ifdef i386
677c478bd9Sstevel@tonic-gate static int	ata_ex_cur(struct defect_list *);
687c478bd9Sstevel@tonic-gate static int	ata_wr_cur(struct defect_list *);
69342440ecSPrasad Singamsetty static int	ata_repair(diskaddr_t, int);
707c478bd9Sstevel@tonic-gate #endif /* i386 */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct  ctlr_ops ataops = {
737c478bd9Sstevel@tonic-gate #if defined(sparc)
747c478bd9Sstevel@tonic-gate 	ata_rdwr,
757c478bd9Sstevel@tonic-gate 	ata_ck_format,
767c478bd9Sstevel@tonic-gate 	0,
777c478bd9Sstevel@tonic-gate 	0,
787c478bd9Sstevel@tonic-gate 	0,
797c478bd9Sstevel@tonic-gate 	0,
807c478bd9Sstevel@tonic-gate 	0,
817c478bd9Sstevel@tonic-gate 	0,
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate 	ata_rdwr,
847c478bd9Sstevel@tonic-gate 	ata_ck_format,
857c478bd9Sstevel@tonic-gate 	0,
867c478bd9Sstevel@tonic-gate 	0,
877c478bd9Sstevel@tonic-gate 	ata_ex_cur,
887c478bd9Sstevel@tonic-gate 	ata_repair,
897c478bd9Sstevel@tonic-gate 	0,
907c478bd9Sstevel@tonic-gate 	ata_wr_cur,
917c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate struct  ctlr_ops pcmcia_ataops = {
957c478bd9Sstevel@tonic-gate 	ata_rdwr,
967c478bd9Sstevel@tonic-gate 	ata_ck_format,
977c478bd9Sstevel@tonic-gate 	0,
987c478bd9Sstevel@tonic-gate 	0,
997c478bd9Sstevel@tonic-gate 	0,
1007c478bd9Sstevel@tonic-gate 	0,
1017c478bd9Sstevel@tonic-gate 	0,
1027c478bd9Sstevel@tonic-gate 	0,
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #if defined(i386)
107342440ecSPrasad Singamsetty static struct	dkl_partition	*dpart = NULL;
1087c478bd9Sstevel@tonic-gate #endif	/* defined(i386) */
1097c478bd9Sstevel@tonic-gate extern	struct	badsec_lst	*badsl_chain;
1107c478bd9Sstevel@tonic-gate extern	int	badsl_chain_cnt;
1117c478bd9Sstevel@tonic-gate extern	struct	badsec_lst	*gbadsl_chain;
1127c478bd9Sstevel@tonic-gate extern	int	gbadsl_chain_cnt;
1137c478bd9Sstevel@tonic-gate extern	struct	alts_mempart	*ap;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static char *dadkrawioerrs[] = {
1167c478bd9Sstevel@tonic-gate 	"cmd was successful",		/* DADKIO_STAT_NO_ERROR */
1177c478bd9Sstevel@tonic-gate 	"device not ready",		/* DADKIO_STAT_NOT_READY */
1187c478bd9Sstevel@tonic-gate 	"error on medium blkno: %d",	/* DADKIO_STAT_MEDIUM_ERROR */
1197c478bd9Sstevel@tonic-gate 	"other hardware error",		/* DADKIO_STAT_HARDWARE_ERROR */
1207c478bd9Sstevel@tonic-gate 	"illegal request",		/* DADKIO_STAT_ILLEGAL_REQUEST */
1217c478bd9Sstevel@tonic-gate 	"illegal block address: %d",	/* DADKIO_STAT_ILLEGAL_ADDRESS */
1227c478bd9Sstevel@tonic-gate 	"device write-protected",	/* DADKIO_STAT_WRITE_PROTECTED	*/
1237c478bd9Sstevel@tonic-gate 	"no response from device",	/* DADKIO_STAT_TIMED_OUT */
1247c478bd9Sstevel@tonic-gate 	"parity error in data",		/* DADKIO_STAT_PARITY */
1257c478bd9Sstevel@tonic-gate 	"error on bus",			/* DADKIO_STAT_BUS_ERROR */
1267c478bd9Sstevel@tonic-gate 	"data recovered via ECC",	/* DADKIO_STAT_SOFT_ERROR */
1277c478bd9Sstevel@tonic-gate 	"no resources for cmd",		/* DADKIO_STAT_NO_RESOURCES */
1287c478bd9Sstevel@tonic-gate 	"device is not formatted",	/* DADKIO_STAT_NOT_FORMATTED */
1297c478bd9Sstevel@tonic-gate 	"device is reserved",		/* DADKIO_STAT_RESERVED */
1307c478bd9Sstevel@tonic-gate 	"feature not supported",	/* DADKIO_STAT_NOT_SUPPORTED */
1317c478bd9Sstevel@tonic-gate 	};
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*ARGSUSED6*/
134a11d78d6SToomas Soome _STATIC int
ata_rdwr(int dir,int fd,diskaddr_t blk64,int secnt,caddr_t bufaddr,int flags,int * xfercntp)1357c478bd9Sstevel@tonic-gate ata_rdwr(int dir, int fd, diskaddr_t blk64, int secnt, caddr_t bufaddr,
136a11d78d6SToomas Soome     int flags, int *xfercntp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int	tmpsec;
1397c478bd9Sstevel@tonic-gate 	struct dadkio_rwcmd	dadkio_rwcmd;
140342440ecSPrasad Singamsetty 	blkaddr_t	blkno;
1417c478bd9Sstevel@tonic-gate 
142342440ecSPrasad Singamsetty 	blkno = (blkaddr_t)blk64;
1437c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&dadkio_rwcmd, sizeof (struct dadkio_rwcmd));
1447c478bd9Sstevel@tonic-gate 
14565908c77Syu, larry liu - Sun Microsystems - Beijing China 	tmpsec = secnt * cur_blksz;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* Doing raw read */
1487c478bd9Sstevel@tonic-gate 	dadkio_rwcmd.cmd = (dir == DIR_READ) ? DADKIO_RWCMD_READ :
149a11d78d6SToomas Soome 	    DADKIO_RWCMD_WRITE;
1507c478bd9Sstevel@tonic-gate 	dadkio_rwcmd.blkaddr = blkno;
1517c478bd9Sstevel@tonic-gate 	dadkio_rwcmd.buflen  = tmpsec;
1527c478bd9Sstevel@tonic-gate 	dadkio_rwcmd.flags   = flags;
1537c478bd9Sstevel@tonic-gate 	dadkio_rwcmd.bufaddr = bufaddr;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	media_error = 0;
1567c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * PCATA requires to use "p0" when calling
1597c478bd9Sstevel@tonic-gate 		 *	DIOCTL_RWCMD ioctl() to read/write the label
1607c478bd9Sstevel@tonic-gate 		 */
1617c478bd9Sstevel@tonic-gate 		(void) close(fd);
1627c478bd9Sstevel@tonic-gate 		(void) open_cur_file(FD_USE_P0_PATH);
1637c478bd9Sstevel@tonic-gate 		fd = cur_file;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DIOCTL_RWCMD, &dadkio_rwcmd) == -1) {
1677c478bd9Sstevel@tonic-gate 		err_print("DIOCTL_RWCMD: %s\n", strerror(errno));
1687c478bd9Sstevel@tonic-gate 		return (1);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
1727c478bd9Sstevel@tonic-gate 		/* Restore cur_file with cur_disk->disk_path */
1737c478bd9Sstevel@tonic-gate 		(void) open_cur_file(FD_USE_CUR_DISK_PATH);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	switch (dadkio_rwcmd.status.status) {
1777c478bd9Sstevel@tonic-gate 	case  DADKIO_STAT_NOT_READY:
1787c478bd9Sstevel@tonic-gate 			disk_error = DISK_STAT_NOTREADY;
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 	case  DADKIO_STAT_RESERVED:
1817c478bd9Sstevel@tonic-gate 			disk_error = DISK_STAT_RESERVED;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 	case  DADKIO_STAT_WRITE_PROTECTED:
1847c478bd9Sstevel@tonic-gate 			disk_error = DISK_STAT_DATA_PROTECT;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 	case DADKIO_STAT_MEDIUM_ERROR:
1877c478bd9Sstevel@tonic-gate 			media_error = 1;
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (dadkio_rwcmd.status.status) {
1927c478bd9Sstevel@tonic-gate 		if ((flags & F_SILENT) == 0)
1937c478bd9Sstevel@tonic-gate 			err_print(dadkrawioerrs[dadkio_rwcmd.status.status],
194a11d78d6SToomas Soome 			    dadkio_rwcmd.status.failed_blk);
1957c478bd9Sstevel@tonic-gate 		return (1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate int
ata_ck_format(void)201*b12aaafbSToomas Soome ata_ck_format(void)
2027c478bd9Sstevel@tonic-gate {
20365908c77Syu, larry liu - Sun Microsystems - Beijing China 	char *bufaddr;
2047c478bd9Sstevel@tonic-gate 	int status;
2057c478bd9Sstevel@tonic-gate 
20665908c77Syu, larry liu - Sun Microsystems - Beijing China 	bufaddr = (char *)zalloc(4 * cur_blksz);
207342440ecSPrasad Singamsetty 	status = ata_rdwr(DIR_READ, cur_file, (diskaddr_t)1, 4,
208342440ecSPrasad Singamsetty 	    (caddr_t)bufaddr, 0, NULL);
2097c478bd9Sstevel@tonic-gate 
21065908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(bufaddr);
21165908c77Syu, larry liu - Sun Microsystems - Beijing China 
2127c478bd9Sstevel@tonic-gate 	return (!status);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #if defined(i386)
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static int
get_alts_slice(void)219*b12aaafbSToomas Soome get_alts_slice(void)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	int	i;
2237c478bd9Sstevel@tonic-gate 	int	alts_slice = -1;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (cur_parts == NULL) {
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "No current partition list\n");
2277c478bd9Sstevel@tonic-gate 		return (-1);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR && alts_slice == -1; i++) {
2317c478bd9Sstevel@tonic-gate 		if (cur_parts->vtoc.v_part[i].p_tag == V_ALTSCTR) {
2327c478bd9Sstevel@tonic-gate 			alts_slice = i;
233342440ecSPrasad Singamsetty 			dpart = &cur_parts->vtoc.v_part[i];
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (alts_slice == -1) {
2387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "NO Alt slice\n");
2397c478bd9Sstevel@tonic-gate 		return (-1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	if (!solaris_offset)
2427c478bd9Sstevel@tonic-gate 		if (copy_solaris_part(&cur_disk->fdisk_part))
2437c478bd9Sstevel@tonic-gate 			return (-1);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	altsec_offset = dpart->p_start + solaris_offset;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static int
put_alts_slice(void)252*b12aaafbSToomas Soome put_alts_slice(void)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	int	status;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	status = wr_altsctr();
2577c478bd9Sstevel@tonic-gate 	if (status) {
2587c478bd9Sstevel@tonic-gate 		return (status);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (ioctl(cur_file, DKIOCADDBAD, NULL) == -1) {
2627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Warning: DKIOCADDBAD ioctl failed\n");
2637c478bd9Sstevel@tonic-gate 		sync();
2647c478bd9Sstevel@tonic-gate 		return (-1);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	sync();
2677c478bd9Sstevel@tonic-gate 	return (0);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int
ata_convert_list(struct defect_list * list,int list_format)2717c478bd9Sstevel@tonic-gate ata_convert_list(struct defect_list *list, int list_format)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	int	i;
2757c478bd9Sstevel@tonic-gate 	struct  defect_entry    *new_defect;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	switch (list_format) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	case BFI_FORMAT:
2807c478bd9Sstevel@tonic-gate 		if (ap->ap_tblp->alts_ent_used) {
2817c478bd9Sstevel@tonic-gate 			new_defect = calloc(ap->ap_tblp->alts_ent_used,
2827c478bd9Sstevel@tonic-gate 			    sizeof (struct defect_entry));
2837c478bd9Sstevel@tonic-gate 			if (new_defect == NULL) {
2847c478bd9Sstevel@tonic-gate 				err_print(
2857c478bd9Sstevel@tonic-gate 				    "ata_convert_list: calloc failed\n");
2867c478bd9Sstevel@tonic-gate 				fullabort();
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 			list->header.count = ap->ap_tblp->alts_ent_used;
2897c478bd9Sstevel@tonic-gate 			list->header.magicno = (uint_t)DEFECT_MAGIC;
2907c478bd9Sstevel@tonic-gate 			list->list = new_defect;
2917c478bd9Sstevel@tonic-gate 			for (i = 0; i < ap->ap_tblp->alts_ent_used;
292342440ecSPrasad Singamsetty 			    i++, new_defect++) {
2937c478bd9Sstevel@tonic-gate 				new_defect->cyl =
294342440ecSPrasad Singamsetty 				    bn2c((ap->ap_entp)[i].bad_start);
2957c478bd9Sstevel@tonic-gate 				new_defect->head =
296342440ecSPrasad Singamsetty 				    bn2h((ap->ap_entp)[i].bad_start);
2977c478bd9Sstevel@tonic-gate 				new_defect->bfi = UNKNOWN;
2987c478bd9Sstevel@tonic-gate 				new_defect->sect =
299342440ecSPrasad Singamsetty 				    bn2s((ap->ap_entp)[i].bad_start);
3007c478bd9Sstevel@tonic-gate 				new_defect->nbits = UNKNOWN;
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		} else {
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			list->header.count = 0;
3077c478bd9Sstevel@tonic-gate 			list->header.magicno = (uint_t)DEFECT_MAGIC;
3087c478bd9Sstevel@tonic-gate 			new_defect = calloc(1,
3097c478bd9Sstevel@tonic-gate 			    sizeof (struct defect_entry));
3107c478bd9Sstevel@tonic-gate 			if (new_defect == NULL) {
3117c478bd9Sstevel@tonic-gate 				err_print(
3127c478bd9Sstevel@tonic-gate 				    "ata_convert_list: calloc failed\n");
3137c478bd9Sstevel@tonic-gate 				fullabort();
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 			list->list = new_defect;
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	default:
3207c478bd9Sstevel@tonic-gate 		err_print("ata_convert_list: can't deal with it\n");
3217c478bd9Sstevel@tonic-gate 		exit(0);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	(void) checkdefsum(list, CK_MAKESUM);
3247c478bd9Sstevel@tonic-gate 	return (0);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * NB - there used to be a ata_ex_man() which was identical to
3307c478bd9Sstevel@tonic-gate  * ata_ex_cur; since it's really not a "manufacturer's list",
3317c478bd9Sstevel@tonic-gate  * it's gone; if we ever want that exact functionality back,
3327c478bd9Sstevel@tonic-gate  * we can add ata_ex_cur() to the ctlr_ops above.  Otherwise,
3337c478bd9Sstevel@tonic-gate  * if this is ever modified to support formatting of IDE drives,
3347c478bd9Sstevel@tonic-gate  * we should probably add something that issues the
3357c478bd9Sstevel@tonic-gate  * drive Read Defect list rather than getting the s9 info
3367c478bd9Sstevel@tonic-gate  * as ata_ex_cur() does.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static int
ata_ex_cur(struct defect_list * list)3407c478bd9Sstevel@tonic-gate ata_ex_cur(struct defect_list *list)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int	status;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	status = get_alts_slice();
3457c478bd9Sstevel@tonic-gate 	if (status)
3467c478bd9Sstevel@tonic-gate 		return (status);
3477c478bd9Sstevel@tonic-gate 	status = read_altsctr(dpart);
3487c478bd9Sstevel@tonic-gate 	if (status) {
3497c478bd9Sstevel@tonic-gate 		return (status);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	(void) ata_convert_list(list, BFI_FORMAT);
3527c478bd9Sstevel@tonic-gate 	return (status);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate int
ata_repair(diskaddr_t bn,int flag)356342440ecSPrasad Singamsetty ata_repair(diskaddr_t bn, int flag)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	int	status;
3607c478bd9Sstevel@tonic-gate 	struct	badsec_lst	*blc_p;
3617c478bd9Sstevel@tonic-gate 	struct	badsec_lst	*blc_p_nxt;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate #ifdef lint
3647c478bd9Sstevel@tonic-gate 	flag++;
3657c478bd9Sstevel@tonic-gate #endif
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	(void) get_alts_slice();
3687c478bd9Sstevel@tonic-gate 	if (!gbadsl_chain) {
3697c478bd9Sstevel@tonic-gate 		blc_p = (struct badsec_lst *)calloc(1, BADSLSZ);
3707c478bd9Sstevel@tonic-gate 		if (!blc_p) {
3717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3727c478bd9Sstevel@tonic-gate 		"Unable to allocate memory for additional bad sectors\n");
3737c478bd9Sstevel@tonic-gate 			return (-1);
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		gbadsl_chain = blc_p;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
3787c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (blc_p->bl_cnt == MAXBLENT) {
3817c478bd9Sstevel@tonic-gate 		blc_p->bl_nxt = (struct badsec_lst *)calloc(1, BADSLSZ);
3827c478bd9Sstevel@tonic-gate 		if (!blc_p->bl_nxt) {
3837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3847c478bd9Sstevel@tonic-gate 		"Unable to allocate memory for additional bad sectors\n");
3857c478bd9Sstevel@tonic-gate 			return (-1);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		blc_p = blc_p->bl_nxt;
3887c478bd9Sstevel@tonic-gate 	}
389342440ecSPrasad Singamsetty 	blc_p->bl_sec[blc_p->bl_cnt++] = (uint_t)bn;
3907c478bd9Sstevel@tonic-gate 	gbadsl_chain_cnt++;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	(void) updatebadsec(dpart, 0);
3937c478bd9Sstevel@tonic-gate 	status = put_alts_slice();
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/* clear out the bad sector list chains that were generated */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (badsl_chain) {
3987c478bd9Sstevel@tonic-gate 		if (badsl_chain->bl_nxt == NULL) {
3997c478bd9Sstevel@tonic-gate 			free(badsl_chain);
4007c478bd9Sstevel@tonic-gate 		} else {
4017c478bd9Sstevel@tonic-gate 			for (blc_p = badsl_chain; blc_p; ) {
4027c478bd9Sstevel@tonic-gate 				blc_p_nxt = blc_p->bl_nxt;
4037c478bd9Sstevel@tonic-gate 				free(blc_p);
4047c478bd9Sstevel@tonic-gate 				blc_p = blc_p_nxt;
4057c478bd9Sstevel@tonic-gate 			}
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 		badsl_chain = NULL;
4087c478bd9Sstevel@tonic-gate 		badsl_chain_cnt = 0;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (gbadsl_chain) {
4127c478bd9Sstevel@tonic-gate 		if (gbadsl_chain->bl_nxt == NULL) {
4137c478bd9Sstevel@tonic-gate 			free(gbadsl_chain);
4147c478bd9Sstevel@tonic-gate 		} else {
4157c478bd9Sstevel@tonic-gate 			for (blc_p = gbadsl_chain; blc_p; ) {
4167c478bd9Sstevel@tonic-gate 				blc_p_nxt = blc_p->bl_nxt;
4177c478bd9Sstevel@tonic-gate 				free(blc_p);
4187c478bd9Sstevel@tonic-gate 				blc_p = blc_p_nxt;
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		gbadsl_chain = NULL;
4227c478bd9Sstevel@tonic-gate 		gbadsl_chain_cnt = 0;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (status);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate int
ata_wr_cur(struct defect_list * list)4307c478bd9Sstevel@tonic-gate ata_wr_cur(struct defect_list *list)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	int	status;
4337c478bd9Sstevel@tonic-gate 	int	sec_count;
4347c478bd9Sstevel@tonic-gate 	int	x;
4357c478bd9Sstevel@tonic-gate 	struct	badsec_lst	*blc_p;
4367c478bd9Sstevel@tonic-gate 	struct	badsec_lst	*blc_p_nxt;
4377c478bd9Sstevel@tonic-gate 	struct	defect_entry	*dlist;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (list->header.magicno != (uint_t)DEFECT_MAGIC)
4407c478bd9Sstevel@tonic-gate 		return (-1);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	sec_count = list->header.count;
4437c478bd9Sstevel@tonic-gate 	dlist = list->list;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	(void) get_alts_slice();
4467c478bd9Sstevel@tonic-gate 	for (x = 0; x < sec_count; x++) {
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		/* test for unsupported list format */
4497c478bd9Sstevel@tonic-gate 		if ((dlist->bfi != UNKNOWN) || (dlist->nbits != UNKNOWN)) {
4507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
451342440ecSPrasad Singamsetty 			    "BFI unsuported format for bad sectors\n");
4527c478bd9Sstevel@tonic-gate 			return (-1);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		if (!gbadsl_chain) {
4567c478bd9Sstevel@tonic-gate 			blc_p = (struct badsec_lst *)calloc(1, BADSLSZ);
4577c478bd9Sstevel@tonic-gate 			if (!blc_p) {
4587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4597c478bd9Sstevel@tonic-gate 		"Unable to allocate memory for additional bad sectors\n");
4607c478bd9Sstevel@tonic-gate 				return (-1);
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 			gbadsl_chain = blc_p;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		for (blc_p = gbadsl_chain; blc_p->bl_nxt; )
4667c478bd9Sstevel@tonic-gate 			blc_p = blc_p->bl_nxt;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		if (blc_p->bl_cnt == MAXBLENT) {
4697c478bd9Sstevel@tonic-gate 			blc_p->bl_nxt = (struct badsec_lst *)calloc(1, BADSLSZ);
4707c478bd9Sstevel@tonic-gate 			if (!blc_p->bl_nxt) {
4717c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4727c478bd9Sstevel@tonic-gate 		"Unable to allocate memory for additional bad sectors\n");
4737c478bd9Sstevel@tonic-gate 				return (-1);
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			blc_p = blc_p->bl_nxt;
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 		blc_p->bl_sec[blc_p->bl_cnt++] =
478342440ecSPrasad Singamsetty 		    (uint_t)chs2bn(dlist->cyl, dlist->head, dlist->sect);
4797c478bd9Sstevel@tonic-gate 		gbadsl_chain_cnt++;
4807c478bd9Sstevel@tonic-gate 		dlist++;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	(void) updatebadsec(dpart, 0);
4857c478bd9Sstevel@tonic-gate 	status = put_alts_slice();
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* clear out the bad sector list chains that were generated */
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (badsl_chain) {
4907c478bd9Sstevel@tonic-gate 		if (badsl_chain->bl_nxt == NULL) {
4917c478bd9Sstevel@tonic-gate 			free(badsl_chain);
4927c478bd9Sstevel@tonic-gate 		} else {
4937c478bd9Sstevel@tonic-gate 			for (blc_p = badsl_chain; blc_p; ) {
4947c478bd9Sstevel@tonic-gate 				blc_p_nxt = blc_p->bl_nxt;
4957c478bd9Sstevel@tonic-gate 				free(blc_p);
4967c478bd9Sstevel@tonic-gate 				blc_p = blc_p_nxt;
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		badsl_chain = NULL;
5007c478bd9Sstevel@tonic-gate 		badsl_chain_cnt = 0;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if (gbadsl_chain) {
5047c478bd9Sstevel@tonic-gate 		if (gbadsl_chain->bl_nxt == NULL) {
5057c478bd9Sstevel@tonic-gate 			free(gbadsl_chain);
5067c478bd9Sstevel@tonic-gate 		} else {
5077c478bd9Sstevel@tonic-gate 			for (blc_p = gbadsl_chain; blc_p; ) {
5087c478bd9Sstevel@tonic-gate 				blc_p_nxt = blc_p->bl_nxt;
5097c478bd9Sstevel@tonic-gate 				free(blc_p);
5107c478bd9Sstevel@tonic-gate 				blc_p = blc_p_nxt;
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		gbadsl_chain = NULL;
5147c478bd9Sstevel@tonic-gate 		gbadsl_chain_cnt = 0;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (status);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate #endif	/*  defined(i386)  */
521