1 /* $NetBSD: st_atapi.c,v 1.31 2016/07/14 04:00:46 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2001 Manuel Bouyer.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: st_atapi.c,v 1.31 2016/07/14 04:00:46 msaitoh Exp $");
29
30 #ifdef _KERNEL_OPT
31 #include "opt_scsi.h"
32 #endif
33
34 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/buf.h>
37 #include <sys/bufq.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41
42 #include <dev/scsipi/stvar.h>
43 #include <dev/scsipi/atapi_tape.h>
44
45 static int st_atapibus_match(device_t, cfdata_t, void *);
46 static void st_atapibus_attach(device_t, device_t, void *);
47 static int st_atapibus_ops(struct st_softc *, int, int);
48 static int st_atapibus_mode_sense(struct st_softc *, int);
49
50 CFATTACH_DECL_NEW(
51 st_atapibus,
52 sizeof(struct st_softc),
53 st_atapibus_match,
54 st_atapibus_attach,
55 stdetach,
56 NULL
57 );
58
59 static const struct scsipi_inquiry_pattern st_atapibus_patterns[] = {
60 {T_SEQUENTIAL, T_REMOV,
61 "", "", ""},
62 };
63
64 static int
st_atapibus_match(device_t parent,cfdata_t match,void * aux)65 st_atapibus_match(device_t parent, cfdata_t match, void *aux)
66 {
67 struct scsipibus_attach_args *sa = aux;
68 int priority;
69
70 if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph)) !=
71 SCSIPI_BUSTYPE_ATAPI)
72 return 0;
73
74 (void)scsipi_inqmatch(&sa->sa_inqbuf,
75 st_atapibus_patterns,
76 sizeof(st_atapibus_patterns)/sizeof(st_atapibus_patterns[0]),
77 sizeof(st_atapibus_patterns[0]), &priority);
78 return priority;
79 }
80
81 static void
st_atapibus_attach(device_t parent,device_t self,void * aux)82 st_atapibus_attach(device_t parent, device_t self, void *aux)
83 {
84 struct st_softc *st = device_private(self);
85 struct scsipibus_attach_args *sa = aux;
86 struct scsipi_periph *periph = sa->sa_periph;
87
88 if (strcmp(sa->sa_inqbuf.vendor, "OnStream DI-30") == 0) {
89 struct ast_identifypage identify;
90 int error;
91
92 error = scsipi_mode_sense(periph, SMS_DBD,
93 ATAPI_TAPE_IDENTIFY_PAGE, &identify.header,
94 sizeof(identify), XS_CTL_DISCOVERY,
95 ST_RETRIES, ST_CTL_TIME);
96 if (error) {
97 aprint_error_dev(self,
98 "onstream get identify: error %d\n", error);
99 return;
100 }
101 strncpy(identify.ident, "NBSD", 4);
102 error = scsipi_mode_select(periph, SMS_PF,
103 &identify.header, sizeof(identify),
104 XS_CTL_DISCOVERY, ST_RETRIES, ST_CTL_TIME);
105 if (error) {
106 aprint_error_dev(self,
107 "onstream set identify: error %d\n", error);
108 return;
109 }
110 }
111
112 st->ops = st_atapibus_ops;
113 stattach(parent, self, aux);
114 }
115
116 static int
st_atapibus_ops(struct st_softc * st,int op,int flags)117 st_atapibus_ops(struct st_softc *st, int op, int flags)
118 {
119 switch(op) {
120 case ST_OPS_RBL:
121 /* done in mode_sense */
122 return 0;
123 case ST_OPS_MODESENSE:
124 return st_atapibus_mode_sense(st, flags);
125 case ST_OPS_MODESELECT:
126 return st_mode_select(st, flags);
127 case ST_OPS_CMPRSS_ON:
128 case ST_OPS_CMPRSS_OFF:
129 return ENODEV;
130 default:
131 panic("st_scsibus_ops: invalid op");
132 /* NOTREACHED */
133 }
134 }
135
136 static int
st_atapibus_mode_sense(struct st_softc * st,int flags)137 st_atapibus_mode_sense(struct st_softc *st, int flags)
138 {
139 int count, error;
140 struct atapi_cappage cappage;
141 struct scsipi_periph *periph = st->sc_periph;
142
143 /* get drive capabilities, some drives needs this repeated */
144 for (count = 0 ; count < 5 ; count++) {
145 error = scsipi_mode_sense(periph, SMS_DBD,
146 ATAPI_TAPE_CAP_PAGE, &cappage.header, sizeof(cappage),
147 flags, ST_RETRIES, ST_CTL_TIME);
148 if (error == 0) {
149 st->numblks = 0; /* unused anyway */
150 if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK32K)
151 st->media_blksize = 32768;
152 else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK1K)
153 st->media_blksize = 1024;
154 else if (cappage.cap4 & ATAPI_TAPE_CAP_PAGE_BLK512)
155 st->media_blksize = 512;
156 else {
157 error = ENODEV;
158 continue;
159 }
160 st->blkmin = st->blkmax = st->media_blksize;
161 st->media_density = 0;
162 if (cappage.header.dev_spec & SMH_DSP_WRITE_PROT)
163 st->flags |= ST_READONLY;
164 else
165 st->flags &= ~ST_READONLY;
166 SC_DEBUG(periph, SCSIPI_DB3,
167 ("density code %d, %d-byte blocks, write-%s, ",
168 st->media_density, st->media_blksize,
169 st->flags & ST_READONLY ? "protected" : "enabled"));
170 SC_DEBUG(periph, SCSIPI_DB3,
171 ("%sbuffered\n",
172 cappage.header.dev_spec & SMH_DSP_BUFF_MODE ?
173 "" : "un"));
174 periph->periph_flags |= PERIPH_MEDIA_LOADED;
175 return 0;
176 }
177 }
178 return error;
179 }
180