Merge from vendor branch GCC:
[dragonfly.git] / sys / bus / cam / cam.c
1 /*
2  * Generic utility routines for the Common Access Method layer.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/cam/cam.c,v 1.3 1999/08/28 00:40:38 peter Exp $
29  * $DragonFly: src/sys/bus/cam/cam.c,v 1.6 2004/02/16 19:43:27 dillon Exp $
30  */
31 #include <sys/param.h>
32 #ifdef _KERNEL
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #else
37 #include <stdlib.h>
38 #include <stdio.h>
39 #endif
40
41 #include "cam.h"
42 #include "cam_ccb.h"
43 #include "scsi/scsi_all.h"
44 #include <sys/sbuf.h>
45
46 #ifdef _KERNEL
47 #include <sys/libkern.h>
48 #include "cam_xpt.h"
49 #endif
50
51 #ifdef _KERNEL
52
53 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
54
55 #endif
56
57 void
58 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
59 {
60
61         /* Trim leading/trailing spaces, nulls. */
62         while (srclen > 0 && src[0] == ' ')
63                 src++, srclen--;
64         while (srclen > 0
65             && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
66                 srclen--;
67
68         while (srclen > 0 && dstlen > 1) {
69                 u_int8_t *cur_pos = dst;
70
71                 if (*src < 0x20 || *src >= 0x80) {
72                         /* SCSI-II Specifies that these should never occur. */
73                         /* non-printable character */
74                         if (dstlen > 4) {
75                                 *cur_pos++ = '\\';
76                                 *cur_pos++ = ((*src & 0300) >> 6) + '0';
77                                 *cur_pos++ = ((*src & 0070) >> 3) + '0';
78                                 *cur_pos++ = ((*src & 0007) >> 0) + '0';
79                         } else {
80                                 *cur_pos++ = '?';
81                         }
82                 } else {
83                         /* normal character */
84                         *cur_pos++ = *src;
85                 }
86                 src++;
87                 srclen--;
88                 dstlen -= cur_pos - dst;
89                 dst = cur_pos;
90         }
91         *dst = '\0';
92 }
93
94 /*
95  * Compare string with pattern, returning 0 on match.
96  * Short pattern matches trailing blanks in name,
97  * wildcard '*' in pattern matches rest of name,
98  * wildcard '?' matches a single non-space character.
99  */
100 int
101 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
102 {
103
104         while (*pattern != '\0'&& str_len > 0) {  
105
106                 if (*pattern == '*') {
107                         return (0);
108                 }
109                 if ((*pattern != *str)
110                  && (*pattern != '?' || *str == ' ')) {
111                         return (1);
112                 }
113                 pattern++;
114                 str++;
115                 str_len--;
116         }
117         while (str_len > 0 && *str++ == ' ')
118                 str_len--;
119
120         return (str_len);
121 }
122
123 caddr_t
124 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
125                int entry_size, cam_quirkmatch_t *comp_func)
126 {
127         for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
128                 if ((*comp_func)(target, quirk_table) == 0)
129                         return (quirk_table);
130         }
131         return (NULL);
132 }
133
134 /*
135  * Common calculate geometry fuction
136  *
137  * Caller should set ccg->volume_size and block_size.
138  * The extended parameter should be zero if extended translation
139  * should not be used.
140  */
141 void
142 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
143 {
144         uint32_t size_mb, secs_per_cylinder;
145
146         size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
147         if (size_mb > 1024 && extended) {
148                 ccg->heads = 255;
149                 ccg->secs_per_track = 63;
150         } else {
151                 ccg->heads = 64;
152                 ccg->secs_per_track = 32;
153         }
154         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
155         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
156         ccg->ccb_h.status = CAM_REQ_CMP;
157 }
158