Home | History | Annotate | Line # | Download | only in libsa
dev_disk.c revision 1.4
      1 /*	$NetBSD: dev_disk.c,v 1.4 2008/04/28 20:23:39 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This module implements a "raw device" interface suitable for
     34  * use by the stand-alone I/O library UFS file-system code, and
     35  * possibly for direct access (i.e. boot from tape).
     36  *
     37  * The implementation is deceptively simple because it uses the
     38  * drivers provided by the Sun PROM monitor.  Note that only the
     39  * PROM driver used to load the boot program is available here.
     40  */
     41 
     42 #include <sys/types.h>
     43 #include <machine/mon.h>
     44 #include <machine/stdarg.h>
     45 #include <stand.h>
     46 
     47 #include "libsa.h"
     48 #include "dvma.h"
     49 #include "saio.h"
     50 #include "dev_disk.h"
     51 
     52 #define RETRY_COUNT 5
     53 
     54 int disk_opencount;
     55 struct saioreq disk_ioreq;
     56 
     57 int
     58 disk_open(struct open_file *f, ...)
     59 {
     60 	struct bootparam *bp;
     61 	struct saioreq *si;
     62 	int error;
     63 
     64 #ifdef DEBUG_PROM
     65 	char *devname;		/* Device part of file name (or NULL). */
     66 	va_list ap;
     67 
     68 	va_start(ap, f);
     69 	devname = va_arg(ap, char *);
     70 	if (debug)
     71 		printf("disk_open: %s\n", devname);
     72 	va_end(ap);
     73 #endif
     74 
     75 	si = &disk_ioreq;
     76 	if (disk_opencount == 0) {
     77 		/*
     78 		 * Setup our part of the saioreq.
     79 		 * (determines what gets opened)
     80 		 */
     81 		bp = *romVectorPtr->bootParam;
     82 		si->si_boottab = bp->bootDevice;
     83 		si->si_ctlr = bp->ctlrNum;
     84 		si->si_unit = bp->unitNum;
     85 		si->si_boff = bp->partNum;
     86 		if ((error = prom_iopen(si)) != 0)
     87 			return (error);
     88 	}
     89 	disk_opencount++;
     90 
     91 	f->f_devdata = si;
     92 	return 0;
     93 }
     94 
     95 int
     96 disk_close(struct open_file *f)
     97 {
     98 	struct saioreq *si;
     99 
    100 #ifdef DEBUG_PROM
    101 	if (debug)
    102 		printf("disk_close: ocnt=%d\n", disk_opencount);
    103 #endif
    104 
    105 	si = f->f_devdata;
    106 	f->f_devdata = NULL;
    107 	if (disk_opencount <= 0)
    108 		return 0;
    109 	if (--disk_opencount == 0)
    110 		prom_iclose(si);
    111 	return 0;
    112 }
    113 
    114 int
    115 disk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf,
    116     size_t *rsize)
    117 {
    118 	struct saioreq *si;
    119 	struct boottab *ops;
    120 	char *dmabuf;
    121 	int retry, si_flag, xcnt;
    122 
    123 	si = devdata;
    124 	ops = si->si_boottab;
    125 
    126 #ifdef DEBUG_PROM
    127 	if (debug > 1)
    128 		printf("disk_strategy: size=%d dblk=%d\n", size, dblk);
    129 #endif
    130 
    131 	dmabuf = dvma_mapin(buf, size);
    132 	si_flag = (flag == F_READ) ? SAIO_F_READ : SAIO_F_WRITE;
    133 
    134 	/*
    135 	 * The PROM strategy will occasionally return -1 and expect
    136 	 * us to try again.  From mouse (at) Collatz.McRCIM.McGill.EDU
    137 	 */
    138 	retry = RETRY_COUNT;
    139 	do {
    140 		si->si_bn = dblk;
    141 		si->si_ma = dmabuf;
    142 		si->si_cc = size;
    143 		xcnt = (*ops->b_strategy)(si, si_flag);
    144 	} while ((xcnt <= 0) && (--retry > 0));
    145 
    146 	dvma_mapout(dmabuf, size);
    147 
    148 #ifdef DEBUG_PROM
    149 	if (debug > 1)
    150 		printf("disk_strategy: xcnt = %x retries=%d\n",
    151 		   xcnt, RETRY_COUNT - retry);
    152 #endif
    153 
    154 	if (xcnt <= 0)
    155 		return (EIO);
    156 
    157 	*rsize = xcnt;
    158 	return (0);
    159 }
    160 
    161 int
    162 disk_ioctl(struct open_file *f, u_long cmd, void *data)
    163 {
    164 	return EIO;
    165 }
    166 
    167